Skip to content

Commit

Permalink
Populate calendar description with team members
Browse files Browse the repository at this point in the history
  • Loading branch information
iansan5653 authored Feb 1, 2024
1 parent 352cd7b commit 87f2e0b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 16 deletions.
8 changes: 2 additions & 6 deletions src/cards/Calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { TeamCalendarId } from "../models/TeamCalendarId";
import { formatGoogleCalendarName, googleCalendarSettingsUrl } from "./utils/googleCalendar";
import { syncStatusText } from "./utils/teamCalendar";
import { QueueFullSyncCalendarAction } from "../endpoints/onQueueFullSyncCalendar";
import { getTeamMemberDisplayName } from "../jobs/getTeamMemberDisplayName";
import { describeTeamMembers } from "../jobs/getTeamMemberDisplayName";

function CalendarHeader(calendar: TeamCalendar) {
return CardService.newCardHeader().setTitle(calendar.name);
Expand Down Expand Up @@ -83,11 +83,7 @@ function CalendarSettingsSection(calendarId: TeamCalendarId, calendar: TeamCalen
.addWidget(
CardService.newDecoratedText()
.setTopLabel("Team members")
.setText(
Object.keys(calendar.teamMembers)
.map((e) => ` - ${getTeamMemberDisplayName(e, calendar.nameFormat)}`)
.join("\n"),
),
.setText(describeTeamMembers(Object.keys(calendar.teamMembers), calendar.nameFormat)),
)
.addWidget(
CardService.newDecoratedText()
Expand Down
21 changes: 18 additions & 3 deletions src/controllers/LinkedCalendarController.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { describeTeamMembers } from "../jobs/getTeamMemberDisplayName";
import type { GoogleCalendarId } from "../models/GoogleCalendarId";
import { TeamCalendar, NewTeamCalendar } from "../models/TeamCalendar";

const formatDescription = (
sourceCalendar: NewTeamCalendar,
) => `Automatically updated with out of office events from the following team members:
${describeTeamMembers(Object.keys(sourceCalendar.teamMembers), sourceCalendar.nameFormat)}`;

export const LinkedCalendarController = {
create(name: string) {
return CalendarApp.createCalendar(name, {
create(sourceCalendar: NewTeamCalendar) {
return CalendarApp.createCalendar(sourceCalendar.name, {
hidden: false,
summary: "Automatically updated team calendar.",
summary: formatDescription(sourceCalendar),
selected: true,
}).getId() as GoogleCalendarId;
},
Expand All @@ -25,4 +33,11 @@ export const LinkedCalendarController = {
return false;
}
},
update(sourceCalendar: TeamCalendar) {
const calendar = this.read(sourceCalendar.googleCalendarId);
if (!calendar) return;

calendar.setName(sourceCalendar.name);
calendar.setDescription(formatDescription(sourceCalendar));
},
};
2 changes: 1 addition & 1 deletion src/jobs/createCalendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { asyncFullSyncCalendar } from "./asyncFullSyncCalendar";
import { updateSyncTriggers } from "./updateSyncTriggers";

export function createCalendar(config: NewTeamCalendar) {
const googleCalendarId = LinkedCalendarController.create(config.name);
const googleCalendarId = LinkedCalendarController.create(config);

const result = TeamCalendarController.create({
...config,
Expand Down
4 changes: 4 additions & 0 deletions src/jobs/getTeamMemberDisplayName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ export function getTeamMemberDisplayName(email: string, format: NameFormat) {
);
}
}

export function describeTeamMembers(teamMembers: string[], nameFormat: NameFormat) {
return teamMembers.map((email) => ` - ${getTeamMemberDisplayName(email, nameFormat)}`).join("\n");
}
2 changes: 1 addition & 1 deletion src/jobs/recreateLinkedCalendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function recreateLinkedCalendar(teamCalendarId: TeamCalendarId) {
const teamCalendar = TeamCalendarController.read(teamCalendarId);
if (!teamCalendar) throw new Error("Team calendar not found");

const googleCalendarId = LinkedCalendarController.create(teamCalendar.name);
const googleCalendarId = LinkedCalendarController.create(teamCalendar);

// clear existing sync states
const unsyncedTeamMembers = Object.fromEntries(
Expand Down
8 changes: 3 additions & 5 deletions src/jobs/updateCalendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import { asyncFullSyncCalendar } from "./asyncFullSyncCalendar";
import { updateSyncTriggers } from "./updateSyncTriggers";

export function updateCalendar(calendarId: TeamCalendarId, fields: Partial<TeamCalendar>) {
const updated = TeamCalendarController.update(calendarId, fields);
const updatedTeamCalendar = TeamCalendarController.update(calendarId, fields);

// Update linked calendar name
if ("name" in fields)
LinkedCalendarController.read(updated.googleCalendarId)?.setName(updated.name);
LinkedCalendarController.update(updatedTeamCalendar);

updateSyncTriggers();
asyncFullSyncCalendar(calendarId);

return updated;
return updatedTeamCalendar;
}

0 comments on commit 87f2e0b

Please sign in to comment.