Skip to content

Commit

Permalink
Apex-1631 - szulinap bekeres ujrapingeles (#15)
Browse files Browse the repository at this point in the history
* feat: birthday be able to null

* chore: rename migration

* feat: save user when ask his birthday

* feat: saveBirthday without birthday date

* feat: add getUsersWhoseBirthdayIsMissing query

* feat: send birthday reminder if it is missing from the db

* fix: reset pnpm-lock

* fix: askBirthday unit test

* fix: team_id when save birhtday

* fix: import url

* feat: add test for getUsersWhoseBirthdayIsMissing query

* feat: integration test to ask again birthday if it is null

* chore: use sendCronEvent instead of fetch

* feat: add team_id and fix unit askBirthday tests

* fix: shouldn't find any users unit test
  • Loading branch information
attilazsoltdanku authored Nov 9, 2023
1 parent 2e683ba commit 1240aa0
Show file tree
Hide file tree
Showing 11 changed files with 333 additions and 40 deletions.
1 change: 1 addition & 0 deletions packages/core/db/migrations/0004_birthday_can_be_null.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "users" ALTER COLUMN "birthday" DROP NOT NULL;
37 changes: 8 additions & 29 deletions packages/core/db/migrations/meta/0003_snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,8 @@
"name": "iceBreakerThreads_user_id_team_id_users_id_team_id_fk",
"tableFrom": "iceBreakerThreads",
"tableTo": "users",
"columnsFrom": [
"user_id",
"team_id"
],
"columnsTo": [
"id",
"team_id"
],
"columnsFrom": ["user_id", "team_id"],
"columnsTo": ["id", "team_id"],
"onDelete": "cascade",
"onUpdate": "no action"
}
Expand Down Expand Up @@ -95,29 +89,17 @@
"name": "presentIdeas_user_id_team_id_users_id_team_id_fk",
"tableFrom": "presentIdeas",
"tableTo": "users",
"columnsFrom": [
"user_id",
"team_id"
],
"columnsTo": [
"id",
"team_id"
],
"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"
],
"columnsFrom": ["birthday_person", "team_id"],
"columnsTo": ["id", "team_id"],
"onDelete": "cascade",
"onUpdate": "no action"
}
Expand Down Expand Up @@ -175,10 +157,7 @@
"compositePrimaryKeys": {
"users_id_team_id": {
"name": "users_id_team_id",
"columns": [
"id",
"team_id"
]
"columns": ["id", "team_id"]
}
},
"uniqueConstraints": {}
Expand All @@ -191,4 +170,4 @@
"tables": {},
"columns": {}
}
}
}
194 changes: 194 additions & 0 deletions packages/core/db/migrations/meta/0004_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
{
"version": "5",
"dialect": "pg",
"id": "f4d596d1-e02b-4c39-98c3-5a48f49b8c65",
"prevId": "3ba6727b-d01d-4de2-a788-d1cdaa365084",
"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": false
}
},
"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 @@ -29,6 +29,13 @@
"when": 1699369077670,
"tag": "0003_present_ideas",
"breakpoints": true
},
{
"idx": 4,
"version": "5",
"when": 1699455923273,
"tag": "0004_birthday_can_be_null",
"breakpoints": true
}
]
}
7 changes: 6 additions & 1 deletion packages/core/db/queries/getBirthdays.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Dayjs } from "dayjs";
import { and, or, sql } from "drizzle-orm";
import { and, isNull, or, sql } from "drizzle-orm";

import { db } from "@/db/index";
import { users } from "@/db/schema";
Expand Down Expand Up @@ -39,3 +39,8 @@ export const getBirthdays = async (date: Dayjs) =>
sql`EXTRACT('DAY' FROM ${users.birthday}) = ${date.date()}`,
),
});

export const getUsersWhoseBirthdayIsMissing = async () =>
db.query.users.findMany({
where: isNull(users.birthday),
});
8 changes: 5 additions & 3 deletions packages/core/db/queries/saveBirthday.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@ import { users } from "@/db/schema";
type Args = {
user: string;
teamId: string;
birthday: string;
birthday: string | null;
};

export const saveBirthday = async ({ birthday, teamId, user }: Args) => {
const birthdayDate = birthday ? dayjs.utc(birthday).toDate() : null;

await db
.insert(users)
.values({
id: user,
teamId,
birthday: dayjs.utc(birthday).toDate(),
birthday: birthdayDate,
})
.onConflictDoUpdate({
target: [users.id, users.teamId],
set: {
birthday: dayjs.utc(birthday).toDate(),
birthday: birthdayDate,
},
});
};
2 changes: 1 addition & 1 deletion packages/core/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const users = pgTable(
{
id: varchar("id").notNull(),
teamId: varchar("team_id").notNull(),
birthday: date("birthday", { mode: "date" }).notNull(),
birthday: date("birthday", { mode: "date" }),
},
(t) => ({
pk: primaryKey(t.id, t.teamId),
Expand Down
20 changes: 19 additions & 1 deletion packages/functions/cron/daily.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,26 @@ import utc from "dayjs/plugin/utc";

dayjs.extend(utc);

import { getBirthdays } from "@/db/queries/getBirthdays";
import {
getBirthdays,
getUsersWhoseBirthdayIsMissing,
} from "@/db/queries/getBirthdays";
import { publishEvent } from "@/utils/eventBridge/publishEvent";
import { cronHandler } from "@/utils/lambda/cronHandler";

const sendReminderWhoseBirthdayIsMissing = async (eventId?: string) => {
const users = await getUsersWhoseBirthdayIsMissing();

await Promise.all(
users.map((user) =>
publishEvent("askBirthday", {
user: user.id,
eventId,
}),
),
);
};

export const handler = cronHandler(async (eventId?: string) => {
try {
const today = dayjs().utc().startOf("day");
Expand All @@ -23,6 +39,8 @@ export const handler = cronHandler(async (eventId?: string) => {
),
);

await sendReminderWhoseBirthdayIsMissing(eventId);

return {
users,
message: users.length ? "Sent present ideas requests" : "No birthdays",
Expand Down
Loading

0 comments on commit 1240aa0

Please sign in to comment.