Skip to content

Commit

Permalink
[Calendar] Fix Tag Relationships (#36)
Browse files Browse the repository at this point in the history
Co-authored-by: EncoreBot <ghbot@encoredigitalgroup.com>
  • Loading branch information
onairmarc and EncoreBot authored Dec 7, 2024
1 parent 8f30729 commit 2a54b5b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
1 change: 1 addition & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ cbe36f817a31ddbeb1ef59120b2406f6e9d4a689
56cf02b78b6c1327e150c1f5f6357cd973371434
fa4b60ada3c0013ece75995fe7d741e5ab3e80b4
99bd1813b52dabee4f7a6e9fde1548dba008870c
0301a888d15da0b7d5749d41a541e9d838ae8a5c
16 changes: 14 additions & 2 deletions src/Objects/Calendar/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace EncoreDigitalGroup\PlanningCenter\Objects\Calendar;

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

$tagRecord = Tag::make($this->clientId, $this->clientSecret);
$clientResponse = new ClientResponse($http);
$tagRecord->mapFromPco($clientResponse);
$records = objectify($clientResponse->meta->response->json("data", []));

if (is_iterable($records)) {
foreach ($records as $record) {
$tagAttributes = new TagAttributes;
$tagAttributes->tagId = $record->id;
AttributeMapper::from($record, $tagAttributes, Tag::getAttributeMap(), ["created_at", "updated_at"]);
$this->relationships->tags->add($tagAttributes);
}

$clientResponse->data->add($this);

}

return $clientResponse;
}
Expand Down
24 changes: 14 additions & 10 deletions src/Objects/Calendar/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ public static function make(string $clientId, string $clientSecret): Tag
return $tagGroup;
}

/** @internal */
public static function getAttributeMap(): array
{
return [
"churchCenterCategory" => "church_center_category",
"color" => "color",
"createdAt" => "created_at",
"name" => "name",
"position" => "position",
"updatedAt" => "updated_at",
];
}

public function forTagGroupId(string $tagGroupId): static
{
$this->tagGroupId = $tagGroupId;
Expand Down Expand Up @@ -56,16 +69,7 @@ public function mapFromPco(ClientResponse $clientResponse): void
foreach ($records as $record) {
$this->attributes->tagId = $record->id;

$attributeMap = [
"churchCenterCategory" => "church_center_category",
"color" => "color",
"createdAt" => "created_at",
"name" => "name",
"position" => "position",
"updatedAt" => "updated_at",
];

AttributeMapper::from($record, $this->attributes, $attributeMap, ["created_at", "updated_at"]);
AttributeMapper::from($record, $this->attributes, self::getAttributeMap(), ["created_at", "updated_at"]);
$clientResponse->data->add($this);
}

Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/Calendar/EventTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@
->forEventId("1")
->tags();

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

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

0 comments on commit 2a54b5b

Please sign in to comment.