diff --git a/api/__tests__/mock/building.ts b/api/__tests__/mock/building.ts index 115a221df..90384296a 100644 --- a/api/__tests__/mock/building.ts +++ b/api/__tests__/mock/building.ts @@ -1,5 +1,4 @@ import { prisma } from "./prisma"; -import { image, manual } from "./file"; export async function initialiseBuilding() { const building1 = { @@ -11,7 +10,7 @@ export async function initialiseBuilding() { manual_id: 1, deleted: false, - syndicus_id: 1, + syndicus_id: 4, hash: "aaaa", }; @@ -24,7 +23,7 @@ export async function initialiseBuilding() { address_id: 2, manual_id: 1, deleted: false, - syndicus_id: 2, + syndicus_id: 1, hash: "abcd", }; @@ -36,7 +35,7 @@ export async function initialiseBuilding() { expected_time: 150, address_id: 3, manual_id: 1, - syndicus_id: 1, + syndicus_id: 4, deleted: true, hash: "klmno", diff --git a/api/__tests__/mock/database.ts b/api/__tests__/mock/database.ts index 1ba26ba0e..e02a0d6c7 100644 --- a/api/__tests__/mock/database.ts +++ b/api/__tests__/mock/database.ts @@ -1,10 +1,6 @@ import { initialiseRegion } from "./region"; import { initialiseAddress } from "./address"; -import { - initialiseSyndicus, - initialiseUser, - initialiseUserRegion, -} from "./user"; +import { initialiseUser, initialiseUserRegion } from "./user"; import { initialiseBuilding, initialiseBuildingImages } from "./building"; import { initialiseProgress, @@ -28,7 +24,6 @@ const initialiseFunctions: { [name: string]: () => Promise } = { files: initialiseFiles, region: initialiseRegion, user_region: initialiseUserRegion, - syndicus: initialiseSyndicus, building: initialiseBuilding, building_image: initialiseBuildingImages, garbage: initialiseGarbage, diff --git a/api/__tests__/mock/user.ts b/api/__tests__/mock/user.ts index 089fc6eac..a760c3991 100644 --- a/api/__tests__/mock/user.ts +++ b/api/__tests__/mock/user.ts @@ -147,17 +147,3 @@ export async function initialiseUserRegion() { data: [entry, entry2], }); } - -export async function initialiseSyndicus() { - const syndicus1 = { - user_id: 4, - }; - - const syndicus2 = { - user_id: 1, - }; - - await prisma.syndicus.createMany({ - data: [syndicus1, syndicus2], - }); -} diff --git a/api/__tests__/routes/building.test.ts b/api/__tests__/routes/building.test.ts index d8b332456..20866b161 100644 --- a/api/__tests__/routes/building.test.ts +++ b/api/__tests__/routes/building.test.ts @@ -75,22 +75,18 @@ describe("Building tests", () => { }, name: "Building 1", syndicus: { - id: 1, - user: { - address_id: 3, - admin: false, - date_added: "2023-05-04T12:00:00.000Z", - deleted: false, - email: "syndicus@trottoir.be", - first_name: "Simon", - id: 4, - last_login: "2023-05-04T12:00:00.000Z", - last_name: "De Syndicus", - phone: "7894561230", - student: false, - super_student: false, - }, - user_id: 4, + address_id: 3, + admin: false, + date_added: "2023-05-04T12:00:00.000Z", + deleted: false, + email: "syndicus@trottoir.be", + first_name: "Simon", + id: 4, + last_login: "2023-05-04T12:00:00.000Z", + last_name: "De Syndicus", + phone: "7894561230", + student: false, + super_student: false, }, }, { @@ -137,22 +133,18 @@ describe("Building tests", () => { }, name: "Building 2", syndicus: { - id: 2, - user: { - address_id: 1, - admin: false, - date_added: "2023-05-04T12:00:00.000Z", - deleted: false, - email: "student@trottoir.be", - first_name: "Dirk", - id: 1, - last_login: "2023-05-04T12:00:00.000Z", - last_name: "De Student", - phone: "0123456789", - student: true, - super_student: false, - }, - user_id: 1, + address_id: 1, + admin: false, + date_added: "2023-05-04T12:00:00.000Z", + deleted: false, + email: "student@trottoir.be", + first_name: "Dirk", + id: 1, + last_login: "2023-05-04T12:00:00.000Z", + last_name: "De Student", + phone: "0123456789", + student: true, + super_student: false, }, }, ]; @@ -208,22 +200,18 @@ describe("Building tests", () => { name: "Building 1", expected_time: 100, syndicus: { - id: 1, - user: { - address_id: 3, - admin: false, - date_added: "2023-05-04T12:00:00.000Z", - deleted: false, - email: "syndicus@trottoir.be", - first_name: "Simon", - id: 4, - last_login: "2023-05-04T12:00:00.000Z", - last_name: "De Syndicus", - phone: "7894561230", - student: false, - super_student: false, - }, - user_id: 4, + address_id: 3, + admin: false, + date_added: "2023-05-04T12:00:00.000Z", + deleted: false, + email: "syndicus@trottoir.be", + first_name: "Simon", + id: 4, + last_login: "2023-05-04T12:00:00.000Z", + last_name: "De Syndicus", + phone: "7894561230", + student: false, + super_student: false, }, }; @@ -307,23 +295,20 @@ describe("Building tests", () => { name: "Building 1", progress: [], syndicus: { - id: 1, - user: { - address_id: 3, - admin: false, - date_added: "2023-05-04T12:00:00.000Z", - deleted: false, - email: "syndicus@trottoir.be", - first_name: "Simon", - id: 4, - last_login: "2023-05-04T12:00:00.000Z", - last_name: "De Syndicus", - phone: "7894561230", - student: false, - super_student: false, - }, - user_id: 4, + address_id: 3, + admin: false, + date_added: "2023-05-04T12:00:00.000Z", + deleted: false, + email: "syndicus@trottoir.be", + first_name: "Simon", + id: 4, + last_login: "2023-05-04T12:00:00.000Z", + last_name: "De Syndicus", + phone: "7894561230", + student: false, + super_student: false, }, + syndicus_id: 4, }; await runner.get({ url: "/building/aaaa", @@ -364,22 +349,18 @@ describe("Building tests", () => { longitude: 151.2099, }, syndicus: { - id: 1, - user_id: 4, - user: { - id: 4, - email: "syndicus@trottoir.be", - first_name: "Simon", - last_name: "De Syndicus", - last_login: "2023-05-04T12:00:00.000Z", - date_added: "2023-05-04T12:00:00.000Z", - phone: "7894561230", - address_id: 3, - student: false, - super_student: false, - admin: false, - deleted: false, - }, + id: 4, + email: "syndicus@trottoir.be", + first_name: "Simon", + last_name: "De Syndicus", + last_login: "2023-05-04T12:00:00.000Z", + date_added: "2023-05-04T12:00:00.000Z", + phone: "7894561230", + address_id: 3, + student: false, + super_student: false, + admin: false, + deleted: false, }, manual: { id: 1, @@ -439,22 +420,18 @@ describe("Building tests", () => { longitude: 151.2099, }, syndicus: { - id: 1, - user_id: 4, - user: { - id: 4, - email: "syndicus@trottoir.be", - first_name: "Simon", - last_name: "De Syndicus", - last_login: "2023-05-04T12:00:00.000Z", - date_added: "2023-05-04T12:00:00.000Z", - phone: "7894561230", - address_id: 3, - student: false, - super_student: false, - admin: false, - deleted: false, - }, + id: 4, + email: "syndicus@trottoir.be", + first_name: "Simon", + last_name: "De Syndicus", + last_login: "2023-05-04T12:00:00.000Z", + date_added: "2023-05-04T12:00:00.000Z", + phone: "7894561230", + address_id: 3, + student: false, + super_student: false, + admin: false, + deleted: false, }, manual: { id: 1, @@ -500,7 +477,7 @@ describe("Building tests", () => { expected_time: 100, address_id: 3, manual_id: 1, - syndicus_id: 1, + syndicus_id: 4, }; const expectedBuilding = { @@ -520,22 +497,18 @@ describe("Building tests", () => { longitude: 3.71847, }, syndicus: { - id: 1, - user_id: 4, - user: { - id: 4, - email: "syndicus@trottoir.be", - first_name: "Simon", - last_name: "De Syndicus", - last_login: "2023-05-04T12:00:00.000Z", - date_added: "2023-05-04T12:00:00.000Z", - phone: "7894561230", - address_id: 3, - student: false, - super_student: false, - admin: false, - deleted: false, - }, + id: 4, + email: "syndicus@trottoir.be", + first_name: "Simon", + last_name: "De Syndicus", + last_login: "2023-05-04T12:00:00.000Z", + date_added: "2023-05-04T12:00:00.000Z", + phone: "7894561230", + address_id: 3, + student: false, + super_student: false, + admin: false, + deleted: false, }, manual: { id: 1, @@ -615,22 +588,18 @@ describe("Building tests", () => { }, name: "Building 2", syndicus: { - id: 2, - user: { - address_id: 1, - admin: false, - date_added: "2023-05-04T12:00:00.000Z", - deleted: false, - email: "student@trottoir.be", - first_name: "Dirk", - id: 1, - last_login: "2023-05-04T12:00:00.000Z", - last_name: "De Student", - phone: "0123456789", - student: true, - super_student: false, - }, - user_id: 1, + address_id: 1, + admin: false, + date_added: "2023-05-04T12:00:00.000Z", + deleted: false, + email: "student@trottoir.be", + first_name: "Dirk", + id: 1, + last_login: "2023-05-04T12:00:00.000Z", + last_name: "De Student", + phone: "0123456789", + student: true, + super_student: false, }, }, { @@ -677,22 +646,18 @@ describe("Building tests", () => { }, name: "Building 3", syndicus: { - id: 1, - user: { - address_id: 3, - admin: false, - date_added: "2023-05-04T12:00:00.000Z", - deleted: false, - email: "syndicus@trottoir.be", - first_name: "Simon", - id: 4, - last_login: "2023-05-04T12:00:00.000Z", - last_name: "De Syndicus", - phone: "7894561230", - student: false, - super_student: false, - }, - user_id: 4, + address_id: 3, + admin: false, + date_added: "2023-05-04T12:00:00.000Z", + deleted: false, + email: "syndicus@trottoir.be", + first_name: "Simon", + id: 4, + last_login: "2023-05-04T12:00:00.000Z", + last_name: "De Syndicus", + phone: "7894561230", + student: false, + super_student: false, }, }, ]; @@ -755,22 +720,18 @@ describe("Building tests", () => { }, name: "Building 1", syndicus: { - id: 1, - user: { - address_id: 3, - admin: false, - date_added: "2023-05-04T12:00:00.000Z", - deleted: false, - email: "syndicus@trottoir.be", - first_name: "Simon", - id: 4, - last_login: "2023-05-04T12:00:00.000Z", - last_name: "De Syndicus", - phone: "7894561230", - student: false, - super_student: false, - }, - user_id: 4, + address_id: 3, + admin: false, + date_added: "2023-05-04T12:00:00.000Z", + deleted: false, + email: "syndicus@trottoir.be", + first_name: "Simon", + id: 4, + last_login: "2023-05-04T12:00:00.000Z", + last_name: "De Syndicus", + phone: "7894561230", + student: false, + super_student: false, }, }, { @@ -817,22 +778,18 @@ describe("Building tests", () => { }, name: "Building 2", syndicus: { - id: 2, - user: { - address_id: 1, - admin: false, - date_added: "2023-05-04T12:00:00.000Z", - deleted: false, - email: "student@trottoir.be", - first_name: "Dirk", - id: 1, - last_login: "2023-05-04T12:00:00.000Z", - last_name: "De Student", - phone: "0123456789", - student: true, - super_student: false, - }, - user_id: 1, + address_id: 1, + admin: false, + date_added: "2023-05-04T12:00:00.000Z", + deleted: false, + email: "student@trottoir.be", + first_name: "Dirk", + id: 1, + last_login: "2023-05-04T12:00:00.000Z", + last_name: "De Student", + phone: "0123456789", + student: true, + super_student: false, }, }, { @@ -879,22 +836,18 @@ describe("Building tests", () => { }, name: "Building 3", syndicus: { - id: 1, - user: { - address_id: 3, - admin: false, - date_added: "2023-05-04T12:00:00.000Z", - deleted: false, - email: "syndicus@trottoir.be", - first_name: "Simon", - id: 4, - last_login: "2023-05-04T12:00:00.000Z", - last_name: "De Syndicus", - phone: "7894561230", - student: false, - super_student: false, - }, - user_id: 4, + address_id: 3, + admin: false, + date_added: "2023-05-04T12:00:00.000Z", + deleted: false, + email: "syndicus@trottoir.be", + first_name: "Simon", + id: 4, + last_login: "2023-05-04T12:00:00.000Z", + last_name: "De Syndicus", + phone: "7894561230", + student: false, + super_student: false, }, }, ]; @@ -956,22 +909,18 @@ describe("Building tests", () => { }, name: "Building 2", syndicus: { - id: 2, - user: { - address_id: 1, - admin: false, - date_added: "2023-05-04T12:00:00.000Z", - deleted: false, - email: "student@trottoir.be", - first_name: "Dirk", - id: 1, - last_login: "2023-05-04T12:00:00.000Z", - last_name: "De Student", - phone: "0123456789", - student: true, - super_student: false, - }, - user_id: 1, + address_id: 1, + admin: false, + date_added: "2023-05-04T12:00:00.000Z", + deleted: false, + email: "student@trottoir.be", + first_name: "Dirk", + id: 1, + last_login: "2023-05-04T12:00:00.000Z", + last_name: "De Student", + phone: "0123456789", + student: true, + super_student: false, }, }, ]; @@ -1046,22 +995,18 @@ describe("Building tests", () => { }, name: "Building 1", syndicus: { - id: 1, - user: { - address_id: 3, - admin: false, - date_added: "2023-05-04T12:00:00.000Z", - deleted: false, - email: "syndicus@trottoir.be", - first_name: "Simon", - id: 4, - last_login: "2023-05-04T12:00:00.000Z", - last_name: "De Syndicus", - phone: "7894561230", - student: false, - super_student: false, - }, - user_id: 4, + address_id: 3, + admin: false, + date_added: "2023-05-04T12:00:00.000Z", + deleted: false, + email: "syndicus@trottoir.be", + first_name: "Simon", + id: 4, + last_login: "2023-05-04T12:00:00.000Z", + last_name: "De Syndicus", + phone: "7894561230", + student: false, + super_student: false, }, }; @@ -1113,22 +1058,18 @@ describe("Building tests", () => { }, name: "Building 1", syndicus: { - id: 1, - user: { - address_id: 3, - admin: false, - date_added: "2023-05-04T12:00:00.000Z", - deleted: false, - email: "syndicus@trottoir.be", - first_name: "Simon", - id: 4, - last_login: "2023-05-04T12:00:00.000Z", - last_name: "De Syndicus", - phone: "7894561230", - student: false, - super_student: false, - }, - user_id: 4, + address_id: 3, + admin: false, + date_added: "2023-05-04T12:00:00.000Z", + deleted: false, + email: "syndicus@trottoir.be", + first_name: "Simon", + id: 4, + last_login: "2023-05-04T12:00:00.000Z", + last_name: "De Syndicus", + phone: "7894561230", + student: false, + super_student: false, }, }; @@ -1147,7 +1088,7 @@ describe("Building tests", () => { expected_time: 100, address_id: 3, manual_id: 1, - syndicus_id: 1, + syndicus_id: 4, }; describe("Can't use any path unauthorized", () => { beforeEach(() => { @@ -1333,7 +1274,7 @@ describe("Building tests", () => { ivago_id: "ivago-new", address_id: 3, manual_id: 3, - syndicus_id: 1, + syndicus_id: 4, hash: "abcd", }; diff --git a/api/__tests__/routes/garbage.test.ts b/api/__tests__/routes/garbage.test.ts index db37cada2..440d186ce 100644 --- a/api/__tests__/routes/garbage.test.ts +++ b/api/__tests__/routes/garbage.test.ts @@ -126,7 +126,7 @@ describe("Garbage tests", () => { ]; await runner.get({ - url: "/garbage?syndicus_id=1", + url: "/garbage?syndicus_id=4", expectedData: expectedData, }); }); diff --git a/api/__tests__/routes/syndicus.test.ts b/api/__tests__/routes/syndicus.test.ts deleted file mode 100644 index b0e469830..000000000 --- a/api/__tests__/routes/syndicus.test.ts +++ /dev/null @@ -1,474 +0,0 @@ -import { describe, test } from "@jest/globals"; -import { AuthenticationLevel, Testrunner } from "../utilities/Testrunner"; -import request from "supertest"; -import app from "../../src/main"; -import { resetDatabase, restoreTables } from "../mock/database"; -import { - badRequestForeignKey, - badRequestResponse, - forbiddenResponse, - notFoundResponse, -} from "../utilities/constants"; - -describe("Syndicus tests", () => { - let runner: Testrunner; - beforeAll(async () => { - const server = request(app); - runner = new Testrunner(server); - - return resetDatabase(); - }); - - afterEach(async () => { - await restoreTables(); - }); - - describe("Successful requests", () => { - beforeEach(() => { - runner.authLevel(AuthenticationLevel.SUPER_STUDENT); - }); - test("GET /syndicus", async () => { - const expected = [ - { - building: [ - { - address: { - city: "Sydney", - id: 1, - latitude: -33.865143, - longitude: 151.2099, - number: 42, - street: "Wallaby Way", - zip_code: 2000, - }, - deleted: false, - id: 1, - ivago_id: "ivago-1", - description: "Description of building 1", - name: "Building 1", - }, - { - address: { - city: "Ghent", - id: 3, - latitude: 51.02776, - longitude: 3.71847, - number: 281, - street: "Krijgslaan", - zip_code: 9000, - }, - deleted: true, - id: 3, - ivago_id: "ivago-3", - description: "Description of building 3", - name: "Building 3", - }, - ], - id: 1, - user: { - address: { - city: "Ghent", - id: 3, - latitude: 51.02776, - longitude: 3.71847, - number: 281, - street: "Krijgslaan", - zip_code: 9000, - }, - address_id: 3, - admin: false, - date_added: "2023-05-04T12:00:00.000Z", - deleted: false, - email: "syndicus@trottoir.be", - first_name: "Simon", - id: 4, - last_login: "2023-05-04T12:00:00.000Z", - last_name: "De Syndicus", - phone: "7894561230", - student: false, - super_student: false, - }, - user_id: 4, - }, - { - building: [ - { - address: { - city: "Ghent", - id: 2, - latitude: 51.04732, - longitude: 3.7282, - number: 25, - street: "Sint-Pietersnieuwstraat", - zip_code: 9000, - }, - deleted: false, - id: 2, - ivago_id: "ivago-2", - description: "Description of building 2", - name: "Building 2", - }, - ], - id: 2, - user: { - address: { - city: "Sydney", - id: 1, - latitude: -33.865143, - longitude: 151.2099, - number: 42, - street: "Wallaby Way", - zip_code: 2000, - }, - address_id: 1, - admin: false, - date_added: "2023-05-04T12:00:00.000Z", - deleted: false, - email: "student@trottoir.be", - first_name: "Dirk", - id: 1, - last_login: "2023-05-04T12:00:00.000Z", - last_name: "De Student", - phone: "0123456789", - student: true, - super_student: false, - }, - user_id: 1, - }, - ]; - - await runner.get({ - url: "/syndicus", - expectedData: expected, - }); - }); - - test("GET /syndicus/:id", async () => { - const expected = [ - { - id: 1, - user_id: 4, - user: { - id: 4, - email: "syndicus@trottoir.be", - first_name: "Simon", - last_name: "De Syndicus", - last_login: "2023-05-04T12:00:00.000Z", - date_added: "2023-05-04T12:00:00.000Z", - phone: "7894561230", - address_id: 3, - address: { - id: 3, - street: "Krijgslaan", - number: 281, - city: "Ghent", - zip_code: 9000, - latitude: 51.02776, - longitude: 3.71847, - }, - student: false, - super_student: false, - admin: false, - deleted: false, - }, - building: [ - { - id: 1, - name: "Building 1", - ivago_id: "ivago-1", - description: "Description of building 1", - deleted: false, - address: { - id: 1, - street: "Wallaby Way", - number: 42, - city: "Sydney", - zip_code: 2000, - latitude: -33.865143, - longitude: 151.2099, - }, - }, - { - id: 3, - name: "Building 3", - ivago_id: "ivago-3", - description: "Description of building 3", - deleted: true, - address: { - id: 3, - street: "Krijgslaan", - number: 281, - city: "Ghent", - zip_code: 9000, - latitude: 51.02776, - longitude: 3.71847, - }, - }, - ], - }, - ]; - await runner.get({ - url: "/syndicus/1", - expectedData: expected, - }); - }); - - test("POST /syndicus", async () => { - const syndicus = { - user_id: 3, - }; - - const expected = { - id: 3, - user_id: 3, - user: { - id: 3, - email: "administrator@trottoir.be", - first_name: "Mario", - last_name: "De Administrator", - last_login: "2023-05-04T12:00:00.000Z", - date_added: "2023-05-04T12:00:00.000Z", - phone: "6549873210", - address_id: 3, - address: { - id: 3, - street: "Krijgslaan", - number: 281, - city: "Ghent", - zip_code: 9000, - latitude: 51.02776, - longitude: 3.71847, - }, - student: false, - super_student: false, - admin: true, - deleted: false, - }, - building: [], - }; - - await runner.post({ - url: "/syndicus", - data: syndicus, - expectedResponse: expected, - }); - }); - - test("PATCH /syndicus/:id", async () => { - const updated = { user_id: 2 }; - const expected = { - id: 1, - user_id: 2, - user: { - id: 2, - email: "superstudent@trottoir.be", - first_name: "Toon", - last_name: "De Superstudent", - last_login: "2023-05-04T12:00:00.000Z", - date_added: "2023-05-04T12:00:00.000Z", - phone: "9876543210", - address_id: 2, - address: { - id: 2, - street: "Sint-Pietersnieuwstraat", - number: 25, - city: "Ghent", - zip_code: 9000, - latitude: 51.04732, - longitude: 3.7282, - }, - student: false, - super_student: true, - admin: false, - deleted: false, - }, - building: [ - { - id: 1, - name: "Building 1", - ivago_id: "ivago-1", - description: "Description of building 1", - deleted: false, - address: { - id: 1, - street: "Wallaby Way", - number: 42, - city: "Sydney", - zip_code: 2000, - latitude: -33.865143, - longitude: 151.2099, - }, - }, - { - id: 3, - name: "Building 3", - ivago_id: "ivago-3", - description: "Description of building 3", - deleted: true, - address: { - id: 3, - street: "Krijgslaan", - number: 281, - city: "Ghent", - zip_code: 9000, - latitude: 51.02776, - longitude: 3.71847, - }, - }, - ], - }; - await runner.patch({ - url: "/syndicus/1", - data: updated, - expectedResponse: expected, - }); - }); - - test("DELETE /syndicus/:id", async () => { - await runner.delete({ - url: "/building/1", - data: { hardDelete: true }, - }); - await runner.delete({ - url: "/building/3", - data: { hardDelete: true }, - }); - await runner.delete({ - url: "/syndicus/1", - }); - }); - }); - - describe("Unsuccessful requests", () => { - beforeEach(() => { - runner.authLevel(AuthenticationLevel.SUPER_STUDENT); - }); - - test("Deleting syndicus who's linked to a building", async () => { - await runner.delete({ - url: "/syndicus/1", - statusCode: 400, - }); - }); - test("Requests using non-existent syndicus", async () => { - const url = "/syndicus/9"; - await runner.get({ - url: url, - expectedData: [notFoundResponse], - statusCode: 404, - }); - - await runner.patch({ - url: url, - data: { user_id: 2 }, - expectedResponse: notFoundResponse, - statusCode: 404, - }); - - await runner.delete({ - url: url, - statusCode: 404, - }); - }); - test("Requests using wrong type", async () => { - const url = "/syndicus/foo"; - await runner.get({ - url: url, - expectedData: [badRequestResponse], - statusCode: 400, - }); - - await runner.patch({ - url: url, - data: { user_id: 2 }, - expectedResponse: badRequestResponse, - statusCode: 400, - }); - - await runner.delete({ - url: url, - statusCode: 400, - }); - }); - test("Assigning wrong type to user_id", async () => { - await runner.patch({ - url: "/syndicus/1", - data: { user_id: "25" }, - expectedResponse: badRequestForeignKey, - statusCode: 400, - }); - }); - describe("Must be correctly authenticated to use any path", () => { - test("Cannot make any requests without authorisation", async () => { - runner.authLevel(AuthenticationLevel.UNAUTHORIZED); - await runner.get({ - url: "/syndicus/1", - expectedData: [forbiddenResponse], - statusCode: 403, - }); - - await runner.get({ - url: "/syndicus", - expectedData: [forbiddenResponse], - statusCode: 403, - }); - - await runner.post({ - url: "/syndicus", - data: { user_id: 5 }, - expectedResponse: forbiddenResponse, - statusCode: 403, - }); - - await runner.patch({ - url: "/syndicus/1", - data: { user_id: 5 }, - expectedResponse: forbiddenResponse, - statusCode: 403, - }); - - await runner.delete({ - url: "/syndicus/1", - statusCode: 403, - }); - }); - test("Cannot make any requests as student", async () => { - runner.authLevel(AuthenticationLevel.STUDENT); - await runner.get({ - url: "/syndicus/1", - expectedData: [forbiddenResponse], - statusCode: 403, - }); - - await runner.get({ - url: "/syndicus", - expectedData: [forbiddenResponse], - statusCode: 403, - }); - - await runner.post({ - url: "/syndicus", - data: { user_id: 5 }, - expectedResponse: forbiddenResponse, - statusCode: 403, - }); - - await runner.patch({ - url: "/syndicus/1", - data: { user_id: 5 }, - expectedResponse: forbiddenResponse, - statusCode: 403, - }); - - await runner.delete({ - url: "/syndicus/1", - statusCode: 403, - }); - }); - }); - }); - - afterAll(() => { - app.close(); - }); -}); diff --git a/api/src/main.ts b/api/src/main.ts index f8eae6298..5bc13a447 100644 --- a/api/src/main.ts +++ b/api/src/main.ts @@ -8,7 +8,6 @@ import "express-async-errors"; import { ErrorHandler } from "./errors/error_handler"; import { ScheduleRouting } from "./routes/schedule"; import { AuthRouting } from "./routes/auth"; -import { SyndicusRouting } from "./routes/syndicus"; import { RoundRouting } from "./routes/round"; import passport from "passport"; import crypto from "crypto"; @@ -126,7 +125,6 @@ app.use("/building", new BuildingRouting().toRouter()); app.use("/schedule", new ScheduleRouting().toRouter()); app.use("/region", new RegionRouting().toRouter()); app.use("/garbage", new GarbageRouting().toRouter()); -app.use("/syndicus", new SyndicusRouting().toRouter()); app.use("/round", new RoundRouting().toRouter()); app.use("/file", new FileRouting().toRouter()); diff --git a/api/src/routes/building.ts b/api/src/routes/building.ts index 5f7b43ac9..619e173dc 100644 --- a/api/src/routes/building.ts +++ b/api/src/routes/building.ts @@ -40,11 +40,7 @@ export class BuildingRouting extends Routing { deleted: true, hash: false, address: true, - syndicus: { - include: { - user: includeUser(false), - }, - }, + syndicus: includeUser(false), manual: true, images: { include: { @@ -194,11 +190,8 @@ export class BuildingRouting extends Routing { deleted: true, hash: false, address: true, - syndicus: { - include: { - user: includeUser(false), - }, - }, + syndicus_id: true, + syndicus: includeUser(false), manual: true, images: { include: { diff --git a/api/src/routes/garbage.ts b/api/src/routes/garbage.ts index d226c1214..d50fc09f1 100644 --- a/api/src/routes/garbage.ts +++ b/api/src/routes/garbage.ts @@ -21,10 +21,7 @@ export class GarbageRouting extends Routing { !req.user?.super_student && !req.user?.admin && !req.user?.student && - req.user?.syndicus.every( - (element) => - element.id !== Parser.number(req.query["syndicus_id"]), - ) + req.user?.id !== Parser.number(req.query["syndicus_id"]) ) { throw new APIError(APIErrorCode.FORBIDDEN); } diff --git a/api/src/routes/syndicus.ts b/api/src/routes/syndicus.ts deleted file mode 100644 index 3c8d58c66..000000000 --- a/api/src/routes/syndicus.ts +++ /dev/null @@ -1,118 +0,0 @@ -import { prisma } from "../prisma"; -import express from "express"; -import { CustomRequest, includeUser, Routing, selectBuilding } from "./routing"; -import { Auth } from "../auth/auth"; -import { Parser } from "../parser"; -import { Prisma } from "@selab-2/groep-1-orm"; -import { Validator } from "../validators/validator"; -import { SyndicusValidator } from "../validators/syndicus.validator"; -import { APIError } from "../errors/api_error"; -import { APIErrorCode } from "../errors/api_error_code"; - -export class SyndicusRouting extends Routing { - private static includes: Prisma.SyndicusInclude = { - user: includeUser(true), - building: selectBuilding(), - }; - - @Auth.authorization({ superStudent: true }) - async getAll(req: CustomRequest, res: express.Response) { - const result = await prisma.syndicus.findMany({ - take: Parser.number(req.query["take"], 1024), - skip: Parser.number(req.query["skip"], 0), - where: { - user: { - last_login: { - lte: Parser.date(req.query["login_before"]), - gte: Parser.date(req.query["login_after"]), - }, - date_added: { - lte: Parser.date(req.query["added_before"]), - gte: Parser.date(req.query["added_after"]), - }, - OR: { - first_name: { - contains: Parser.string( - req.query["first_name"], - "", - ), - }, - last_name: { - contains: Parser.string(req.query["last_name"], ""), - }, - }, - }, - user_id: Parser.number(req.query["user"]), - }, - include: SyndicusRouting.includes, - orderBy: Parser.order(req.query["sort"], req.query["ord"]), - }); - - return res.status(200).json(result); - } - - @Auth.authorization({ superStudent: true, syndicus: true }) - async getOne(req: CustomRequest, res: express.Response) { - // A syndicus is only allowed to see his own entries - if ( - !req.user?.super_student && - !req.user?.admin && - req.user?.syndicus.every( - (element) => element.id !== Parser.number(req.params["id"]), - ) - ) { - throw new APIError(APIErrorCode.FORBIDDEN); - } - - const result = await prisma.syndicus.findUniqueOrThrow({ - where: { - id: Parser.number(req.params["id"]), - }, - include: SyndicusRouting.includes, - }); - - return res.status(200).json(result); - } - - @Auth.authorization({ superStudent: true }) - async createOne(req: CustomRequest, res: express.Response) { - const syndicus = await prisma.syndicus.create({ - data: { - user_id: parseInt(req.body["user_id"]), - }, - include: SyndicusRouting.includes, - }); - - return res.status(201).json(syndicus); - } - - @Auth.authorization({ superStudent: true }) - async updateOne(req: CustomRequest, res: express.Response) { - const result = await prisma.syndicus.update({ - data: { - user_id: parseInt(req.body["user_id"]), - }, - where: { - id: Parser.number(req.params["id"]), - }, - include: SyndicusRouting.includes, - }); - - return res.status(200).json(result); - } - - @Auth.authorization({ superStudent: true }) - async deleteOne(req: CustomRequest, res: express.Response) { - await prisma.syndicus.delete({ - where: { - id: Parser.number(req.params["id"]), - }, - }); - - return res.status(200).json({}); - } - - getValidator(): Validator { - return new SyndicusValidator(); - } -} diff --git a/api_query/src/building.ts b/api_query/src/building.ts index bd5ec1b27..a5d7c3040 100644 --- a/api_query/src/building.ts +++ b/api_query/src/building.ts @@ -43,11 +43,7 @@ type BuildingAllInfo = Prisma.BuildingGetPayload<{ deleted: true; hash: false; address: true; - syndicus: { - include: { - user: typeof includeUserWithoutAddress; - }; - }; + syndicus: typeof includeUserWithoutAddress; manual: true; images: { include: { diff --git a/api_query/src/index.ts b/api_query/src/index.ts index 16ad0ce93..f87b8c5cc 100644 --- a/api_query/src/index.ts +++ b/api_query/src/index.ts @@ -8,7 +8,6 @@ import { RoundQuery } from "./round"; import { MailTemplateQuery } from "./mail_template"; import { RoundBuildingQuery } from "./round_building"; import { ScheduleQuery } from "./schedule"; -import { SyndicusQuery } from "./syndicus"; import { UserQuery } from "./user"; import { UserRegionQuery } from "./user_region"; import { QueryError } from "./query_error"; @@ -51,7 +50,6 @@ export { RoundQuery, RoundBuildingQuery, ScheduleQuery, - SyndicusQuery, UserQuery, UserRegionQuery, QueryError, diff --git a/api_query/src/syndicus.ts b/api_query/src/syndicus.ts deleted file mode 100644 index f8a7b7f7f..000000000 --- a/api_query/src/syndicus.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { Syndicus, Prisma } from "@selab-2/groep-1-orm"; -import { Query } from "./query"; -import { includeUserWithAddress, includeBuilding } from "./include"; - -export type SyndicusQueryParameters = { - take: number; - skip: number; - login_before: number; - login_after: number; - added_before: Date; - added_after: Date; - name: string; - user: number; - sort: string[]; - ord: Array<"asc" | "desc">; -}; - -type SyndicusAllInfo = Prisma.SyndicusGetPayload<{ - include: { - user: typeof includeUserWithAddress; - building: typeof includeBuilding; - }; -}>; - -export class SyndicusQuery extends Query< - SyndicusQueryParameters, - Syndicus, - SyndicusAllInfo -> { - endpoint = "syndicus"; -} diff --git a/orm/prisma/schema.prisma b/orm/prisma/schema.prisma index a3f2e0c9c..54d94aaaf 100644 --- a/orm/prisma/schema.prisma +++ b/orm/prisma/schema.prisma @@ -51,9 +51,9 @@ model User { // Relaties regions UserRegion[] - syndicus Syndicus[] schedule Schedule[] files File[] + syndicus Building[] @@map("user") } @@ -142,7 +142,7 @@ model Building { expected_time Int? syndicus_id Int - syndicus Syndicus @relation(fields: [syndicus_id], references: [id], onDelete: Restrict) + syndicus User @relation(fields: [syndicus_id], references: [id], onDelete: Restrict) address_id Int address Address @relation(fields: [address_id], references: [id], onDelete: Restrict) @@ -163,18 +163,6 @@ model Building { @@map("building") } -// Dit model houdt alle gebruikers bij die de rol van syndicus hebben. -model Syndicus { - id Int @id @default(autoincrement()) - - user_id Int - user User @relation(fields: [user_id], references: [id], onDelete: Cascade) - - building Building[] - - @@map("syndicus") -} - // Dit model bevat een gebouw, het tijdstip dat het afval daar opgehaald wordt en // een (speciale) actie voor het afval (waarin ook het type afval vermeld staat). model Garbage { diff --git a/web/src/assets/scripts/templateParser.ts b/web/src/assets/scripts/templateParser.ts index 3cd32326a..190034b94 100644 --- a/web/src/assets/scripts/templateParser.ts +++ b/web/src/assets/scripts/templateParser.ts @@ -7,15 +7,15 @@ export function parseTemplate( const variables: { name: string; description: string }[] = [ { name: "\\$\\(syndicus_voornaam\\)", - description: building.syndicus.user.first_name, + description: building.syndicus.first_name, }, { name: "\\$\\(syndicus_achternaam\\)", - description: building.syndicus.user.last_name, + description: building.syndicus.last_name, }, { name: "\\$\\(syndicus_naam\\)", - description: `${building.syndicus.user.first_name} ${building.syndicus.user.last_name}`, + description: `${building.syndicus.first_name} ${building.syndicus.last_name}`, }, { name: "\\$\\(gebouw_naam\\)", description: building.name }, { diff --git a/web/src/components/cards/BuildingCard.vue b/web/src/components/cards/BuildingCard.vue index 2c1672764..2f33d4caa 100644 --- a/web/src/components/cards/BuildingCard.vue +++ b/web/src/components/cards/BuildingCard.vue @@ -140,13 +140,8 @@ const progressItems: Ref>> = ref([]); // We compute the name of the syndicus once. const syndicusName: string | null = (() => { - const syndicus = props.building?.syndicus.user; - - if (syndicus === undefined) { - return null; - } - - return `${syndicus.first_name} ${syndicus.last_name}`; + const syndicus = props.building?.syndicus; + return syndicus ? `${syndicus.first_name} ${syndicus.last_name}` : null; })(); // We compute the full address once. diff --git a/web/src/types/Building.ts b/web/src/types/Building.ts index 1cab79579..a411eff89 100644 --- a/web/src/types/Building.ts +++ b/web/src/types/Building.ts @@ -15,7 +15,7 @@ export class Building implements TableEntity> { name: "", fit: true, get: (e: Result) => - e.syndicus?.user.first_name + " " + e.syndicus?.user.last_name, + e.syndicus?.first_name + " " + e.syndicus?.last_name, type: RowType.AVATAR, sortable: false, }, @@ -24,7 +24,7 @@ export class Building implements TableEntity> { name: "Syndicus", fit: false, get: (e: Result) => - e.syndicus?.user.first_name + " " + e.syndicus?.user.last_name, + e.syndicus?.first_name + " " + e.syndicus?.last_name, type: RowType.TEXT, sortable: true, }, diff --git a/web/src/views/building/BuildingFollowup.vue b/web/src/views/building/BuildingFollowup.vue index 17711f928..92e092b6d 100644 --- a/web/src/views/building/BuildingFollowup.vue +++ b/web/src/views/building/BuildingFollowup.vue @@ -43,8 +43,7 @@ const filter_data = ref({ let buildings: Ref[]> = ref([]); function full_syndicus_name(building: Result) { - const user = building.syndicus?.user; - return user?.first_name + " " + user?.last_name; + return `${building.syndicus.first_name} ${building.syndicus.last_name}`; } function full_address(building: Result) { diff --git a/web/src/views/building/BuildingMaker.vue b/web/src/views/building/BuildingMaker.vue index 1c8fa6622..e261e0c84 100644 --- a/web/src/views/building/BuildingMaker.vue +++ b/web/src/views/building/BuildingMaker.vue @@ -233,7 +233,6 @@ import { AddressQuery, BuildingQuery, UserQuery, - SyndicusQuery, FileQuery, } from "@selab-2/groep-1-query"; import { File, BuildingImages } from "@selab-2/groep-1-orm"; @@ -251,8 +250,6 @@ const possibleUsers = await new UserQuery().getAll({ super_student: false, }); -const syndici = await new SyndicusQuery().getAll(); - const address = ref({ street: "", number: "0", @@ -318,7 +315,7 @@ if (edit) { } async function setFields(requestedBuilding: Result) { - tryOrAlertAsync(async () => { + await tryOrAlertAsync(async () => { const thisSyndicus = requestedBuilding.syndicus; const thisAddress = requestedBuilding.address; @@ -327,8 +324,8 @@ async function setFields(requestedBuilding: Result) { building.value.ivago_id = requestedBuilding.ivago_id; building.value.syndicus = { id: thisSyndicus.id, - first_name: thisSyndicus.user.first_name, - last_name: thisSyndicus.user.last_name, + first_name: thisSyndicus.first_name, + last_name: thisSyndicus.last_name, }; building.value.description = requestedBuilding.description ? requestedBuilding.description @@ -357,7 +354,7 @@ async function addSoloImage() { if (fullBuilding.value) { const newBuilding: Result = await new BuildingQuery().createImage(fullBuilding.value, "soloImage"); - setFields(newBuilding); + await setFields(newBuilding); } } @@ -386,22 +383,9 @@ const submit = () => { }; if (!edit) { - const isNewSyndicus: boolean = syndici.some( - (s: Result) => { - return s.user_id === buildingUnwrapped.syndicus.id; - }, - ); - // Create address const newAddress = await new AddressQuery().createOne(formattedAddress); - if (isNewSyndicus) { - let { id: syndicusId } = await new SyndicusQuery().createOne({ - user_id: buildingUnwrapped.syndicus.id, - }); - buildingUnwrapped.syndicus.id = syndicusId; - } - const file = await new FileQuery().createOne("manual-id"); const newBuilding = await new BuildingQuery().createOne({ @@ -433,18 +417,6 @@ const submit = () => { ...formattedAddress, }); - let syndicus = ( - await new SyndicusQuery().getAll({ - user: buildingUnwrapped.syndicus.id, - }) - ).pop(); - - if (!syndicus) { - syndicus = await new SyndicusQuery().createOne({ - user_id: buildingUnwrapped.syndicus.id, - }); - } - const newBuilding = await new BuildingQuery().updateOne({ id: buildingId.value, name: buildingUnwrapped.name, @@ -453,7 +425,7 @@ const submit = () => { ivago_id: buildingUnwrapped.ivago_id, expected_time: buildingUnwrapped.expectedTimeInHours * 60, manual_id: building.value.manual_id, - syndicus_id: syndicus.id, + syndicus_id: buildingUnwrapped.syndicus.id, }); await router.push(`/gebouw/${newBuilding.id}`); diff --git a/web/src/views/building/BuildingScreen.vue b/web/src/views/building/BuildingScreen.vue index fbb89b78a..6098ec286 100644 --- a/web/src/views/building/BuildingScreen.vue +++ b/web/src/views/building/BuildingScreen.vue @@ -98,9 +98,9 @@
@@ -109,9 +109,9 @@

{{ - building.syndicus?.user.first_name + + building.syndicus?.first_name + " " + - building.syndicus?.user.last_name + building.syndicus?.last_name }}

@@ -121,9 +121,9 @@
diff --git a/web/src/views/contact/ContactSyndicus.vue b/web/src/views/contact/ContactSyndicus.vue index 714107485..9b03ad0e1 100644 --- a/web/src/views/contact/ContactSyndicus.vue +++ b/web/src/views/contact/ContactSyndicus.vue @@ -145,7 +145,7 @@ function updateFields() { function sendMail() { tryOrAlertAsync(async () => { await new MailQuery().createOne({ - to: building.value?.syndicus.user.email, + to: building.value?.syndicus.email, subject: subject.value, content: content.value, });