Skip to content

Commit

Permalink
Small fixes (#31)
Browse files Browse the repository at this point in the history
* fix: send loading indicator before publishing next event

* chore: add logging

* fix: reduce min db capacity

* chore: bump tsx

* fix: remove min db capacity

* fix: run pr check on pr reopen
  • Loading branch information
BaDo2001 authored Dec 22, 2023
1 parent 7e9f26e commit 2a18bbf
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 77 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: PR checks

on:
pull_request:
types: [opened, synchronize]
types: [opened, synchronize, reopened]
branches: [main]

jobs:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"mockdate": "^3.0.5",
"pg": "^8.11.3",
"prettier": "^3.0.3",
"tsx": "^3.14.0",
"tsx": "^4.6.2",
"typescript": "^5.2.2",
"vite": "^4.5.0",
"vitest": "^0.34.6"
Expand Down
4 changes: 4 additions & 0 deletions packages/core/db/queries/deleteBirthdayInputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { db } from "@/db/index";
import { iceBreakerThreads, presentIdeas, squadJoins } from "@/db/schema";

export const deleteBirthdayInputs = async (teamId: string, userId: string) => {
console.log("Deleting birthday inputs", teamId, userId);

await db.transaction(async (tx) => {
await tx
.delete(iceBreakerThreads)
Expand Down Expand Up @@ -32,4 +34,6 @@ export const deleteBirthdayInputs = async (teamId: string, userId: string) => {
),
);
});

console.log("Birthday inputs deleted", teamId, userId);
};
4 changes: 4 additions & 0 deletions packages/core/db/queries/deleteUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { db } from "@/db/index";
import { users } from "@/db/schema";

export const deleteUser = async (userId: string, teamId: string) => {
console.log("Deleting user", userId, teamId);

await db
.delete(users)
.where(and(eq(users.id, userId), eq(users.teamId, teamId)));

console.log("User deleted", userId, teamId);
};
4 changes: 4 additions & 0 deletions packages/core/db/queries/saveBirthday.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type Arguments = {
};

export const saveBirthday = async ({ birthday, teamId, user }: Arguments) => {
console.log("Saving birthday", user, birthday, teamId);

const birthdayDate = birthday ? dayjs.utc(birthday).toDate() : null;

await db
Expand All @@ -28,4 +30,6 @@ export const saveBirthday = async ({ birthday, teamId, user }: Arguments) => {
birthday: birthdayDate,
},
});

console.log("Birthday saved", user, birthday, teamId);
};
4 changes: 4 additions & 0 deletions packages/core/db/queries/savePresentIdea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ import type { PresentIdea } from "@/db/schema";
import { presentIdeas } from "@/db/schema";

export const savePresentIdea = async (presentIdea: PresentIdea) => {
console.log("Saving present idea", presentIdea);

await db.insert(presentIdeas).values(presentIdea);

console.log("Present idea saved", presentIdea);
};
4 changes: 4 additions & 0 deletions packages/core/db/queries/saveSquadJoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ import type { SquadJoin } from "@/db/schema";
import { squadJoins } from "@/db/schema";

export const saveSquadJoin = async (squadJoin: SquadJoin) => {
console.log("Saving squad join", squadJoin);

await db.insert(squadJoins).values(squadJoin);

console.log("Squad join saved", squadJoin);
};
5 changes: 5 additions & 0 deletions packages/core/types/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,8 @@ export type Events = z.infer<typeof Events>;
export type EventType = keyof Events;

export const eventTypes = Object.keys(Events.shape) as EventType[];

export type EventPayload<T extends EventType> = {
type: T;
payload: Events[T];
};
178 changes: 108 additions & 70 deletions packages/functions/lambdas/slack-interaction.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { APIGatewayProxyHandlerV2 } from "aws-lambda";

import type { EventPayload, EventType } from "@/events";
import { constructLoadingMessage } from "@/services/slack/constructLoadingMessage";
import { sendResponse } from "@/services/slack/sendResponse";
import type { SlackInteractionRequest } from "@/types/SlackInteractionRequest";
import {
additionalPresentIdeasInputActionId,
additionalPresentIdeasInputBlockId,
Expand All @@ -20,86 +22,122 @@ import { publishEvent } from "@/utils/eventBridge/publishEvent";
import { parseRequest } from "@/utils/lambda/parseRequest";
import { errorResult, okResult } from "@/utils/lambda/result";

export const handler: APIGatewayProxyHandlerV2 = async (request) => {
try {
const payload = parseRequest(request);
const getEventPayload = (
request: SlackInteractionRequest,
): EventPayload<EventType> | EventPayload<EventType>[] | null => {
switch (request.actions[0].action_id) {
case pickBirthdayActionId:
return {
type: "birthdayFilled",
payload: {
birthday: request.actions[0].selected_date,
responseUrl: request.response_url,
},
};
case birthdayConfirmActionId:
return {
type: "birthdayConfirmed",
payload: {
user: request.user.id,
team: request.user.team_id,
birthday: request.actions[0].value,
responseUrl: request.response_url,
},
};
case birthdayIncorrectActionId:
return {
type: "askBirthday",
payload: {
user: request.user.id,
responseUrl: request.response_url,
},
};
case presentIdeasSaveButtonActionId: {
const presentIdea =
request.state?.values?.[presentIdeasInputBlockId]?.[
presentIdeasInputActionId
]?.value;

const parsedData = SlackInteractionRequestSchema.parse(payload);
if (!presentIdea) {
throw new Error("Present idea is empty");
}

switch (parsedData.actions[0].action_id) {
case pickBirthdayActionId:
await publishEvent("birthdayFilled", {
birthday: parsedData.actions[0].selected_date,
responseUrl: parsedData.response_url,
});
break;
case birthdayConfirmActionId:
await publishEvent("birthdayConfirmed", {
user: parsedData.user.id,
team: parsedData.user.team_id,
birthday: parsedData.actions[0].value,
responseUrl: parsedData.response_url,
});
break;
case birthdayIncorrectActionId:
await publishEvent("askBirthday", {
user: parsedData.user.id,
responseUrl: parsedData.response_url,
});
break;
case presentIdeasSaveButtonActionId: {
const presentIdea =
parsedData.state?.values?.[presentIdeasInputBlockId]?.[
presentIdeasInputActionId
]?.value;

if (!presentIdea) {
throw new Error("Present idea is empty");
}
await publishEvent("savePresentIdea", {
birthdayPerson: parsedData.actions[0].value,
user: parsedData.user.id,
team: parsedData.user.team_id,
return {
type: "savePresentIdea",
payload: {
birthdayPerson: request.actions[0].value,
user: request.user.id,
team: request.user.team_id,
presentIdea,
responseUrl: parsedData.response_url,
});
break;
responseUrl: request.response_url,
},
};
}
case additionalPresentIdeasSaveButtonActionId: {
const presentIdea =
request.state?.values?.[additionalPresentIdeasInputBlockId]?.[
additionalPresentIdeasInputActionId
]?.value;
const squadJoin =
!!request.state?.values?.[squadJoinCheckboxBlockId]?.[
squadJoinCheckboxActionId
]?.selected_options?.length;

if (!presentIdea) {
throw new Error("Additional Present idea is empty");
}
case additionalPresentIdeasSaveButtonActionId: {
const presentIdea =
parsedData.state?.values?.[additionalPresentIdeasInputBlockId]?.[
additionalPresentIdeasInputActionId
]?.value;
const squadJoin =
!!parsedData.state?.values?.[squadJoinCheckboxBlockId]?.[
squadJoinCheckboxActionId
]?.selected_options?.length;

if (!presentIdea) {
throw new Error("Additional Present idea is empty");
}
if (squadJoin) {
await publishEvent("saveSquadJoin", {
birthdayPerson: parsedData.actions[0].value,
user: parsedData.user.id,
team: parsedData.user.team_id,
});
}
await publishEvent("savePresentIdea", {
birthdayPerson: parsedData.actions[0].value,
user: parsedData.user.id,
team: parsedData.user.team_id,
presentIdea,
responseUrl: parsedData.response_url,

const events: EventPayload<EventType>[] = [
{
type: "savePresentIdea",
payload: {
birthdayPerson: request.actions[0].value,
user: request.user.id,
team: request.user.team_id,
presentIdea,
responseUrl: request.response_url,
},
},
];

if (squadJoin) {
events.push({
type: "saveSquadJoin",
payload: {
birthdayPerson: request.actions[0].value,
user: request.user.id,
team: request.user.team_id,
},
});
break;
}
default:
return okResult();

return events;
}
default:
return null;
}
};

export const handler: APIGatewayProxyHandlerV2 = async (request) => {
try {
const payload = parseRequest(request);

const parsedData = SlackInteractionRequestSchema.parse(payload);

const event = getEventPayload(parsedData);

if (!event) {
return okResult();
}

await sendResponse(parsedData.response_url, constructLoadingMessage());

if (Array.isArray(event)) {
await Promise.all(event.map((e) => publishEvent(e.type, e.payload)));
} else {
await publishEvent(event.type, event.payload);
}

return okResult();
} catch (error) {
console.error(`Error handling slack interaction: ${error}`);
Expand Down
2 changes: 2 additions & 0 deletions packages/functions/utils/eventBridge/publishEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export const publishEvent = async <T extends EventType>(
payload: Events[T],
) => {
try {
console.log("Publishing event", event, payload);

await client.send(
new PutEventsCommand({
Entries: [
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2a18bbf

Please sign in to comment.