Skip to content

Commit

Permalink
fix: mongo db prisma schema and related changes for id type
Browse files Browse the repository at this point in the history
  • Loading branch information
vaibhavarora14 committed Jun 5, 2022
1 parent addd212 commit fd6e59f
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 13 deletions.
8 changes: 4 additions & 4 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ 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[]
}

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[]
}

Expand All @@ -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
}
4 changes: 2 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/list/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const createList: Middleware<SlackCommandMiddlewareArgs> = 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({
Expand Down
2 changes: 1 addition & 1 deletion src/rotation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const createRotation: Middleware<SlackCommandMiddlewareArgs> = 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');
Expand Down
6 changes: 3 additions & 3 deletions src/rotation/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -22,7 +22,7 @@ const getRotationMembers = async (rotationId: number) => {
return members.map(member => member.id);
}

const getUserFromRotation = async (users: any[], usersCount: number = 0): Promise<Array<any>> => {
const getUserFromRotation = async (users: any[], usersCount: number = 1): Promise<Array<any>> => {
await database.$connect();

const usersInRotationLogs = await database.rotationLog.findMany({
Expand All @@ -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);
}


Expand Down
4 changes: 2 additions & 2 deletions src/rotationLog/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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
}
})
Expand Down

0 comments on commit fd6e59f

Please sign in to comment.