Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APEX-1560 - Save present ideas to db #14

Merged
merged 10 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/core/db/migrations/0003_present_ideas.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CREATE TABLE IF NOT EXISTS "presentIdeas" (
"id" serial PRIMARY KEY NOT NULL,
"team_id" varchar NOT NULL,
"birthday_person" varchar NOT NULL,
"user_id" varchar NOT NULL,
"present_idea" varchar NOT NULL
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "presentIdeas" ADD CONSTRAINT "presentIdeas_user_id_team_id_users_id_team_id_fk" FOREIGN KEY ("user_id","team_id") REFERENCES "users"("id","team_id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "presentIdeas" ADD CONSTRAINT "presentIdeas_birthday_person_team_id_users_id_team_id_fk" FOREIGN KEY ("birthday_person","team_id") REFERENCES "users"("id","team_id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
194 changes: 194 additions & 0 deletions packages/core/db/migrations/meta/0003_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
{
"version": "5",
"dialect": "pg",
"id": "3ba6727b-d01d-4de2-a788-d1cdaa365084",
"prevId": "0e3e6461-74a7-40d3-b391-88d430dd1bee",
"tables": {
"iceBreakerThreads": {
"name": "iceBreakerThreads",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"team_id": {
"name": "team_id",
"type": "varchar",
"primaryKey": false,
"notNull": true
},
"user_id": {
"name": "user_id",
"type": "varchar",
"primaryKey": false,
"notNull": true
},
"thread_id": {
"name": "thread_id",
"type": "varchar",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {
"iceBreakerThreads_user_id_team_id_users_id_team_id_fk": {
"name": "iceBreakerThreads_user_id_team_id_users_id_team_id_fk",
"tableFrom": "iceBreakerThreads",
"tableTo": "users",
"columnsFrom": [
"user_id",
"team_id"
],
"columnsTo": [
"id",
"team_id"
],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"presentIdeas": {
"name": "presentIdeas",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"team_id": {
"name": "team_id",
"type": "varchar",
"primaryKey": false,
"notNull": true
},
"birthday_person": {
"name": "birthday_person",
"type": "varchar",
"primaryKey": false,
"notNull": true
},
"user_id": {
"name": "user_id",
"type": "varchar",
"primaryKey": false,
"notNull": true
},
"present_idea": {
"name": "present_idea",
"type": "varchar",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {
"presentIdeas_user_id_team_id_users_id_team_id_fk": {
"name": "presentIdeas_user_id_team_id_users_id_team_id_fk",
"tableFrom": "presentIdeas",
"tableTo": "users",
"columnsFrom": [
"user_id",
"team_id"
],
"columnsTo": [
"id",
"team_id"
],
"onDelete": "cascade",
"onUpdate": "no action"
},
"presentIdeas_birthday_person_team_id_users_id_team_id_fk": {
"name": "presentIdeas_birthday_person_team_id_users_id_team_id_fk",
"tableFrom": "presentIdeas",
"tableTo": "users",
"columnsFrom": [
"birthday_person",
"team_id"
],
"columnsTo": [
"id",
"team_id"
],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"testItems": {
"name": "testItems",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "varchar",
"primaryKey": true,
"notNull": true
},
"payload": {
"name": "payload",
"type": "varchar",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"users": {
"name": "users",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "varchar",
"primaryKey": false,
"notNull": true
},
"team_id": {
"name": "team_id",
"type": "varchar",
"primaryKey": false,
"notNull": true
},
"birthday": {
"name": "birthday",
"type": "date",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {
"users_id_team_id": {
"name": "users_id_team_id",
"columns": [
"id",
"team_id"
]
}
},
"uniqueConstraints": {}
}
},
"enums": {},
"schemas": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
}
}
7 changes: 7 additions & 0 deletions packages/core/db/migrations/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
"when": 1698854266897,
"tag": "0002_icebreaker_threads",
"breakpoints": true
},
{
"idx": 3,
"version": "5",
"when": 1699369077670,
"tag": "0003_present_ideas",
"breakpoints": true
}
]
}
12 changes: 12 additions & 0 deletions packages/core/db/queries/savePresentIdea.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";

dayjs.extend(utc);

import { db } from "@/db/index";
import type { PresentIdea } from "@/db/schema";
import { presentIdeas } from "@/db/schema";

export const savePresentIdea = async (presentIdea: PresentIdea) => {
await db.insert(presentIdeas).values(presentIdea);
};
23 changes: 23 additions & 0 deletions packages/core/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,29 @@ export const iceBreakerThreads = pgTable(
}),
);

export const presentIdeas = pgTable(
"presentIdeas",
{
id: serial("id").primaryKey().notNull(),
teamId: varchar("team_id").notNull(),
birthdayPerson: varchar("birthday_person").notNull(),
userId: varchar("user_id").notNull(),
presentIdea: varchar("present_idea").notNull(),
},
(t) => ({
userReference: foreignKey({
columns: [t.userId, t.teamId],
foreignColumns: [users.id, users.teamId],
}).onDelete("cascade"),
birthdayPersonReference: foreignKey({
columns: [t.birthdayPerson, t.teamId],
foreignColumns: [users.id, users.teamId],
}).onDelete("cascade"),
}),
);

export type PresentIdea = typeof presentIdeas.$inferInsert;

export const testItems = pgTable("testItems", {
id: varchar("id").primaryKey().notNull(),
payload: varchar("payload").notNull(),
Expand Down
7 changes: 7 additions & 0 deletions packages/core/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ const Events = z.object({
birthdayPerson: z.string(),
user: z.string(),
}),
savePresentIdea: z.object({
birthdayPerson: z.string(),
user: z.string(),
team: z.string(),
presentIdea: z.string(),
responseUrl: z.string(),
}),
});

export type Events = z.infer<typeof Events>;
Expand Down
27 changes: 15 additions & 12 deletions packages/core/services/slack/constructAskBirthdayMessage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ChatPostMessageArguments } from "@slack/web-api";

import type { ChatReplaceMessageArguments } from "@/types/ChatReplaceMessageArguments";
import type {
PostMessageArguments,
ReplaceMessageArguments,
} from "@/types/MessageArguments";
import { pickBirthdayActionId } from "@/types/SlackInteractionRequest";

import { makeTextBlock, makeTextBlockWithDatepicker } from "./messageItems";
Expand All @@ -14,7 +15,7 @@ type Arguments = {
const constructBaseAskBirthdayMessage = ({
name,
eventId,
}: Arguments): Omit<ChatPostMessageArguments, "channel"> => ({
}: Arguments): Omit<PostMessageArguments, "channel"> => ({
text: "Please share your birthday with us! 🥳",
blocks: [
makeTextBlock(`Hey ${name}! 👋`),
Expand All @@ -23,24 +24,26 @@ const constructBaseAskBirthdayMessage = ({
pickBirthdayActionId,
),
],
metadata: {
event_type: "askBirthday",
event_payload: {
eventId,
},
},
metadata: eventId
? {
event_type: "askBirthday",
event_payload: {
eventId,
},
}
: undefined,
});

export const constructAskBirthdayMessage = (
args: Arguments,
): ChatPostMessageArguments => ({
): PostMessageArguments => ({
...constructBaseAskBirthdayMessage(args),
channel: args.user,
});

export const constructAskBirthdayMessageReplacement = (
args: Arguments,
): ChatReplaceMessageArguments => ({
): ReplaceMessageArguments => ({
...constructBaseAskBirthdayMessage(args),
replace_original: true,
});
Loading
Loading