diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 3991c02..489179e 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -16,9 +16,9 @@ model List { } model User { - id String @id @map("_id") @db.ObjectId + id String @id @map("_id") List List? @relation(fields: [listId], references: [id]) - listId Int? + listId String? RotationLog RotationLog[] } @@ -26,7 +26,7 @@ model Rotation { id String @id @default(auto()) @map("_id") @db.ObjectId name String list List @relation(fields: [listId], references: [id]) - listId Int + listId String RotationLog RotationLog[] } @@ -36,5 +36,5 @@ model RotationLog { user User @relation(fields: [userId], references: [id]) rotation Rotation @relation(fields: [rotationId], references: [id]) userId String - rotationId Int + rotationId String } diff --git a/src/app.ts b/src/app.ts index df57cdd..76e1280 100644 --- a/src/app.ts +++ b/src/app.ts @@ -19,13 +19,13 @@ app.command('/pickup', addRotationLog, createList, createRotation, async ({ comm if (isCommandUsingList(command.text)) { // add list usage logic const { value, text } = getOptionFromText(command.text, 'list'); - const members = await getListMembers(Number(value)); + const members = await getListMembers(value); usersIds = members; command.text = text; } else if (isCommandWithRotation) { // add rotation usage logic const { value, text } = getOptionFromText(command.text, 'rotation'); - const members = await getRotationMembers(Number(value)); + const members = await getRotationMembers(value); usersIds = members; command.text = text; } diff --git a/src/list/list.ts b/src/list/list.ts index ae8fdf0..29600bc 100644 --- a/src/list/list.ts +++ b/src/list/list.ts @@ -80,7 +80,7 @@ const createList: Middleware = async ({ command, say await say(`List - '${list.id}' created. Use this list anytime for picking up a random member`); } -const getListMembers = async (listId: number) => { +const getListMembers = async (listId: string) => { await database.$connect(); const members = await database.list.findFirst({ diff --git a/src/rotation/index.ts b/src/rotation/index.ts index 3c9e81a..5cb3573 100644 --- a/src/rotation/index.ts +++ b/src/rotation/index.ts @@ -11,7 +11,7 @@ const createRotation: Middleware = async ({ command, const text = command.text.replace(createRotationRegex, '').trim(); const { value, text: updatedText } = getOptionFromText(text, 'list'); - const list = await database.list.findFirst({ where: { id: Number(value) } }); + const list = await database.list.findFirst({ where: { id: value } }); if (!list) { return ack('Please enter valid list id for this rotation command'); diff --git a/src/rotation/utilities.ts b/src/rotation/utilities.ts index 6ed46bf..48cc25c 100644 --- a/src/rotation/utilities.ts +++ b/src/rotation/utilities.ts @@ -9,7 +9,7 @@ const isCommandUsingRotation = (command: string) => { } } -const getRotationMembers = async (rotationId: number) => { +const getRotationMembers = async (rotationId: string) => { await database.$connect(); const members = await database.rotation.findFirst({ @@ -22,7 +22,7 @@ const getRotationMembers = async (rotationId: number) => { return members.map(member => member.id); } -const getUserFromRotation = async (users: any[], usersCount: number = 0): Promise> => { +const getUserFromRotation = async (users: any[], usersCount: number = 1): Promise> => { await database.$connect(); const usersInRotationLogs = await database.rotationLog.findMany({ @@ -38,7 +38,7 @@ const getUserFromRotation = async (users: any[], usersCount: number = 0): Promis const usersWithCount = users.map(user => ({ user, count: usersInRotationLogs.filter(userInRotiationLog => userInRotiationLog.user.id === user.id).length })) usersWithCount.sort((a, b) => a.count - b.count); - return usersWithCount.slice(0, usersCount + 1).map(userWithCount => userWithCount.user); + return usersWithCount.slice(0, usersCount).map(userWithCount => userWithCount.user); } diff --git a/src/rotationLog/index.ts b/src/rotationLog/index.ts index c9680b8..b1c4072 100644 --- a/src/rotationLog/index.ts +++ b/src/rotationLog/index.ts @@ -11,7 +11,7 @@ const addRotationLog = async ({ command, ack, next, client }: any) => { const text = command.text.replace(addRotationLogRegex, '').trim(); let { value: rotationId, text: updatedText } = getOptionFromText(text, 'rotation'); - const rotation = await database.rotation.findFirst({ where: { id: Number(rotationId) } }); + const rotation = await database.rotation.findFirst({ where: { id: rotationId } }); if (!rotation) { return ack('Please enter valid rotation id for this rotation command'); @@ -32,7 +32,7 @@ const addRotationLog = async ({ command, ack, next, client }: any) => { await database.rotationLog.create({ data: { date: new Date(), - rotationId: Number(rotationId), + rotationId: rotationId, userId } })