diff --git a/prisma/migrations/20241113023437_add_mailing_list_user_relation/migration.sql b/prisma/migrations/20241113023437_add_mailing_list_user_relation/migration.sql new file mode 100644 index 0000000..302b2d7 --- /dev/null +++ b/prisma/migrations/20241113023437_add_mailing_list_user_relation/migration.sql @@ -0,0 +1,17 @@ +-- CreateTable +CREATE TABLE "_MailingListToUser" ( + "A" INTEGER NOT NULL, + "B" INTEGER NOT NULL +); + +-- CreateIndex +CREATE UNIQUE INDEX "_MailingListToUser_AB_unique" ON "_MailingListToUser"("A", "B"); + +-- CreateIndex +CREATE INDEX "_MailingListToUser_B_index" ON "_MailingListToUser"("B"); + +-- AddForeignKey +ALTER TABLE "_MailingListToUser" ADD CONSTRAINT "_MailingListToUser_A_fkey" FOREIGN KEY ("A") REFERENCES "MailingList"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "_MailingListToUser" ADD CONSTRAINT "_MailingListToUser_B_fkey" FOREIGN KEY ("B") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index be385e3..cdee737 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -24,16 +24,17 @@ model Absence { } model User { - id Int @id @default(autoincrement()) - authId String @unique - email String @unique + id Int @id @default(autoincrement()) + authId String @unique + email String @unique firstName String lastName String - role Role @default(TEACHER) - status Status @default(INVITED) - numOfAbsences Int @default(10) - absences Absence[] @relation("absentTeacher") - substitutes Absence[] @relation("substituteTeacher") + role Role @default(TEACHER) + status Status @default(INVITED) + numOfAbsences Int @default(10) + absences Absence[] @relation("absentTeacher") + substitutes Absence[] @relation("substituteTeacher") + mailingLists MailingList[] } model Location { @@ -47,6 +48,7 @@ model MailingList { id Int @id @default(autoincrement()) name String emails String[] + users User[] } model Subject { diff --git a/prisma/seed/.snaplet/dataModel.json b/prisma/seed/.snaplet/dataModel.json index d4547f2..90fad44 100644 --- a/prisma/seed/.snaplet/dataModel.json +++ b/prisma/seed/.snaplet/dataModel.json @@ -141,8 +141,12 @@ "isRequired": true, "kind": "object", "relationName": "AbsenceToLocation", - "relationFromFields": ["locationId"], - "relationToFields": ["id"], + "relationFromFields": [ + "locationId" + ], + "relationToFields": [ + "id" + ], "isList": false, "isId": false, "isGenerated": false, @@ -155,8 +159,12 @@ "isRequired": true, "kind": "object", "relationName": "AbsenceToSubject", - "relationFromFields": ["subjectId"], - "relationToFields": ["id"], + "relationFromFields": [ + "subjectId" + ], + "relationToFields": [ + "id" + ], "isList": false, "isId": false, "isGenerated": false, @@ -169,8 +177,12 @@ "isRequired": true, "kind": "object", "relationName": "Absence_absentTeacherIdToUser", - "relationFromFields": ["absentTeacherId"], - "relationToFields": ["id"], + "relationFromFields": [ + "absentTeacherId" + ], + "relationToFields": [ + "id" + ], "isList": false, "isId": false, "isGenerated": false, @@ -183,8 +195,12 @@ "isRequired": false, "kind": "object", "relationName": "Absence_substituteTeacherIdToUser", - "relationFromFields": ["substituteTeacherId"], - "relationToFields": ["id"], + "relationFromFields": [ + "substituteTeacherId" + ], + "relationToFields": [ + "id" + ], "isList": false, "isId": false, "isGenerated": false, @@ -195,7 +211,9 @@ "uniqueConstraints": [ { "name": "Absence_pkey", - "fields": ["id"], + "fields": [ + "id" + ], "nullNotDistinct": false } ] @@ -343,7 +361,9 @@ "uniqueConstraints": [ { "name": "Location_pkey", - "fields": ["id"], + "fields": [ + "id" + ], "nullNotDistinct": false } ] @@ -398,12 +418,28 @@ "hasDefaultValue": false, "isId": false, "maxLength": null + }, + { + "name": "_MailingListToUser", + "type": "_MailingListToUser", + "isRequired": false, + "kind": "object", + "relationName": "_MailingListToUserToMailingList", + "relationFromFields": [], + "relationToFields": [], + "isList": true, + "isId": false, + "isGenerated": false, + "sequence": false, + "hasDefaultValue": false } ], "uniqueConstraints": [ { "name": "MailingList_pkey", - "fields": ["id"], + "fields": [ + "id" + ], "nullNotDistinct": false } ] @@ -505,7 +541,9 @@ "uniqueConstraints": [ { "name": "Subject_pkey", - "fields": ["id"], + "fields": [ + "id" + ], "nullNotDistinct": false } ] @@ -658,22 +696,251 @@ "isGenerated": false, "sequence": false, "hasDefaultValue": false + }, + { + "name": "_MailingListToUser", + "type": "_MailingListToUser", + "isRequired": false, + "kind": "object", + "relationName": "_MailingListToUserToUser", + "relationFromFields": [], + "relationToFields": [], + "isList": true, + "isId": false, + "isGenerated": false, + "sequence": false, + "hasDefaultValue": false } ], "uniqueConstraints": [ { "name": "User_pkey", - "fields": ["id"], + "fields": [ + "id" + ], "nullNotDistinct": false }, { "name": "User_authId_key", - "fields": ["authId"], + "fields": [ + "authId" + ], "nullNotDistinct": false }, { "name": "User_email_key", - "fields": ["email"], + "fields": [ + "email" + ], + "nullNotDistinct": false + } + ] + }, + "_MailingListToUser": { + "id": "public._MailingListToUser", + "schemaName": "public", + "tableName": "_MailingListToUser", + "fields": [ + { + "id": "public._MailingListToUser.A", + "name": "A", + "columnName": "A", + "type": "int4", + "isRequired": true, + "kind": "scalar", + "isList": false, + "isGenerated": false, + "sequence": false, + "hasDefaultValue": false, + "isId": true, + "maxLength": null + }, + { + "id": "public._MailingListToUser.B", + "name": "B", + "columnName": "B", + "type": "int4", + "isRequired": true, + "kind": "scalar", + "isList": false, + "isGenerated": false, + "sequence": false, + "hasDefaultValue": false, + "isId": true, + "maxLength": null + }, + { + "name": "MailingList", + "type": "MailingList", + "isRequired": true, + "kind": "object", + "relationName": "_MailingListToUserToMailingList", + "relationFromFields": [ + "A" + ], + "relationToFields": [ + "id" + ], + "isList": false, + "isId": false, + "isGenerated": false, + "sequence": false, + "hasDefaultValue": false + }, + { + "name": "User", + "type": "User", + "isRequired": true, + "kind": "object", + "relationName": "_MailingListToUserToUser", + "relationFromFields": [ + "B" + ], + "relationToFields": [ + "id" + ], + "isList": false, + "isId": false, + "isGenerated": false, + "sequence": false, + "hasDefaultValue": false + } + ], + "uniqueConstraints": [ + { + "name": "_MailingListToUser_AB_unique", + "fields": [ + "A", + "B" + ], + "nullNotDistinct": false + } + ] + }, + "_prisma_migrations": { + "id": "public._prisma_migrations", + "schemaName": "public", + "tableName": "_prisma_migrations", + "fields": [ + { + "id": "public._prisma_migrations.id", + "name": "id", + "columnName": "id", + "type": "varchar", + "isRequired": true, + "kind": "scalar", + "isList": false, + "isGenerated": false, + "sequence": false, + "hasDefaultValue": false, + "isId": true, + "maxLength": 36 + }, + { + "id": "public._prisma_migrations.checksum", + "name": "checksum", + "columnName": "checksum", + "type": "varchar", + "isRequired": true, + "kind": "scalar", + "isList": false, + "isGenerated": false, + "sequence": false, + "hasDefaultValue": false, + "isId": false, + "maxLength": 64 + }, + { + "id": "public._prisma_migrations.finished_at", + "name": "finished_at", + "columnName": "finished_at", + "type": "timestamptz", + "isRequired": false, + "kind": "scalar", + "isList": false, + "isGenerated": false, + "sequence": false, + "hasDefaultValue": false, + "isId": false, + "maxLength": null + }, + { + "id": "public._prisma_migrations.migration_name", + "name": "migration_name", + "columnName": "migration_name", + "type": "varchar", + "isRequired": true, + "kind": "scalar", + "isList": false, + "isGenerated": false, + "sequence": false, + "hasDefaultValue": false, + "isId": false, + "maxLength": 255 + }, + { + "id": "public._prisma_migrations.logs", + "name": "logs", + "columnName": "logs", + "type": "text", + "isRequired": false, + "kind": "scalar", + "isList": false, + "isGenerated": false, + "sequence": false, + "hasDefaultValue": false, + "isId": false, + "maxLength": null + }, + { + "id": "public._prisma_migrations.rolled_back_at", + "name": "rolled_back_at", + "columnName": "rolled_back_at", + "type": "timestamptz", + "isRequired": false, + "kind": "scalar", + "isList": false, + "isGenerated": false, + "sequence": false, + "hasDefaultValue": false, + "isId": false, + "maxLength": null + }, + { + "id": "public._prisma_migrations.started_at", + "name": "started_at", + "columnName": "started_at", + "type": "timestamptz", + "isRequired": true, + "kind": "scalar", + "isList": false, + "isGenerated": false, + "sequence": false, + "hasDefaultValue": true, + "isId": false, + "maxLength": null + }, + { + "id": "public._prisma_migrations.applied_steps_count", + "name": "applied_steps_count", + "columnName": "applied_steps_count", + "type": "int4", + "isRequired": true, + "kind": "scalar", + "isList": false, + "isGenerated": false, + "sequence": false, + "hasDefaultValue": true, + "isId": false, + "maxLength": null + } + ], + "uniqueConstraints": [ + { + "name": "_prisma_migrations_pkey", + "fields": [ + "id" + ], "nullNotDistinct": false } ] @@ -832,4 +1099,4 @@ ] } } -} +} \ No newline at end of file diff --git a/src/pages/api/users/[id].ts b/src/pages/api/users/[id].ts index 17f2344..d96202a 100644 --- a/src/pages/api/users/[id].ts +++ b/src/pages/api/users/[id].ts @@ -13,11 +13,15 @@ export default async function handler( if (req.method === 'GET') { const getAbsences = req.query.getAbsences === 'true'; + const getMailingLists = req.query.getMailingLists === 'true'; try { const user = await prisma.user.findUnique({ where: { id }, - include: { absences: getAbsences }, + include: { + absences: getAbsences, + mailingLists: getMailingLists, + }, }); if (!user) { diff --git a/src/pages/api/users/email/[email].ts b/src/pages/api/users/email/[email].ts index 1037190..26459e4 100644 --- a/src/pages/api/users/email/[email].ts +++ b/src/pages/api/users/email/[email].ts @@ -17,7 +17,9 @@ export default async function handler( res: NextApiResponse ) { const params = req.query; - const shouldIncludeAbsences = params.shouldIncludeAbsences === 'true'; + const getAbsences = params.getAbsences === 'true'; + const getMailingLists = req.query.getMailingLists === 'true'; + const realEmail = params.email as string; try { const useFake = true; @@ -31,7 +33,10 @@ export default async function handler( const user = await prisma.user.findUniqueOrThrow({ where: { email }, - include: { absences: shouldIncludeAbsences }, + include: { + absences: getAbsences, + mailingLists: getMailingLists, + }, }); return res.status(200).json(user);