Skip to content

Commit 2a54b5b

Browse files
onairmarcEncoreBot
andauthored
[Calendar] Fix Tag Relationships (#36)
Co-authored-by: EncoreBot <ghbot@encoredigitalgroup.com>
1 parent 8f30729 commit 2a54b5b

File tree

4 files changed

+33
-16
lines changed

4 files changed

+33
-16
lines changed

.git-blame-ignore-revs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ cbe36f817a31ddbeb1ef59120b2406f6e9d4a689
99
56cf02b78b6c1327e150c1f5f6357cd973371434
1010
fa4b60ada3c0013ece75995fe7d741e5ab3e80b4
1111
99bd1813b52dabee4f7a6e9fde1548dba008870c
12+
0301a888d15da0b7d5749d41a541e9d838ae8a5c

src/Objects/Calendar/Event.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace EncoreDigitalGroup\PlanningCenter\Objects\Calendar;
99

1010
use EncoreDigitalGroup\PlanningCenter\Objects\Calendar\Attributes\EventAttributes;
11+
use EncoreDigitalGroup\PlanningCenter\Objects\Calendar\Attributes\TagAttributes;
1112
use EncoreDigitalGroup\PlanningCenter\Objects\Calendar\Relationships\EventInstanceRelationships;
1213
use EncoreDigitalGroup\PlanningCenter\Objects\Calendar\Relationships\EventRelationships;
1314
use EncoreDigitalGroup\PlanningCenter\Objects\SdkObjects\ClientResponse;
@@ -86,9 +87,20 @@ public function tags(array $query = []): ClientResponse
8687
$http = $this->client()
8788
->get($this->hostname() . self::EVENT_ENDPOINT . "/{$this->attributes->eventId}/tags", $query);
8889

89-
$tagRecord = Tag::make($this->clientId, $this->clientSecret);
9090
$clientResponse = new ClientResponse($http);
91-
$tagRecord->mapFromPco($clientResponse);
91+
$records = objectify($clientResponse->meta->response->json("data", []));
92+
93+
if (is_iterable($records)) {
94+
foreach ($records as $record) {
95+
$tagAttributes = new TagAttributes;
96+
$tagAttributes->tagId = $record->id;
97+
AttributeMapper::from($record, $tagAttributes, Tag::getAttributeMap(), ["created_at", "updated_at"]);
98+
$this->relationships->tags->add($tagAttributes);
99+
}
100+
101+
$clientResponse->data->add($this);
102+
103+
}
92104

93105
return $clientResponse;
94106
}

src/Objects/Calendar/Tag.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ public static function make(string $clientId, string $clientSecret): Tag
2929
return $tagGroup;
3030
}
3131

32+
/** @internal */
33+
public static function getAttributeMap(): array
34+
{
35+
return [
36+
"churchCenterCategory" => "church_center_category",
37+
"color" => "color",
38+
"createdAt" => "created_at",
39+
"name" => "name",
40+
"position" => "position",
41+
"updatedAt" => "updated_at",
42+
];
43+
}
44+
3245
public function forTagGroupId(string $tagGroupId): static
3346
{
3447
$this->tagGroupId = $tagGroupId;
@@ -56,16 +69,7 @@ public function mapFromPco(ClientResponse $clientResponse): void
5669
foreach ($records as $record) {
5770
$this->attributes->tagId = $record->id;
5871

59-
$attributeMap = [
60-
"churchCenterCategory" => "church_center_category",
61-
"color" => "color",
62-
"createdAt" => "created_at",
63-
"name" => "name",
64-
"position" => "position",
65-
"updatedAt" => "updated_at",
66-
];
67-
68-
AttributeMapper::from($record, $this->attributes, $attributeMap, ["created_at", "updated_at"]);
72+
AttributeMapper::from($record, $this->attributes, self::getAttributeMap(), ["created_at", "updated_at"]);
6973
$clientResponse->data->add($this);
7074
}
7175

tests/Unit/Calendar/EventTests.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@
5959
->forEventId("1")
6060
->tags();
6161

62-
/** @var Tag $tag */
63-
$tag = $response->data->first();
62+
/** @var Event $pcoEvent */
63+
$pcoEvent = $response->data->first();
6464

6565
expect($response)->toBeInstanceOf(ClientResponse::class)
6666
->and($response->data)->toBeInstanceOf(Collection::class)
6767
->and($response->data->count())->toBe(1)
68-
->and($tag->attributes->tagId)->toBe(CalendarMocks::TAG_ID)
69-
->and($tag->attributes->name)->toBe(CalendarMocks::TAG_NAME);
68+
->and($pcoEvent->relationships->tags[0]->tagId)->toBe(CalendarMocks::TAG_ID)
69+
->and($pcoEvent->relationships->tags[0]->name)->toBe(CalendarMocks::TAG_NAME);
7070
});
7171
})->group("calendar.event");

0 commit comments

Comments
 (0)