Skip to content

Commit

Permalink
Merge pull request #14 from Prybrt06/add_update_event_optimised
Browse files Browse the repository at this point in the history
Add update event optimised and isAdmin route created
  • Loading branch information
JOSU9435 authored Jan 18, 2024
2 parents 912948f + 5bf6bb8 commit c0b308e
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 7,159 deletions.
21 changes: 10 additions & 11 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ jobs:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2
- name: Checkout
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20.x'
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: "20.x"

- name: Install Dependencies
run: npm i -g pnpm && pnpm install --ignore-scripts

- name: Run Linting
run: pnpm run lint
- name: Install Dependencies
run: npm i -g pnpm && pnpm install --ignore-scripts

- name: Run Linting
run: pnpm run lint
7,087 changes: 0 additions & 7,087 deletions package-lock 2.json

This file was deleted.

3 changes: 2 additions & 1 deletion src/controllers/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { signUp } from "./signUp";
import { checkAdmin } from "./isAdmin";

export { signUp };
export { signUp, checkAdmin };
8 changes: 8 additions & 0 deletions src/controllers/auth/isAdmin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as Interfaces from "@interfaces";
import * as Utils from "@utils";

const checkAdmin: Interfaces.Controller.Async = async (_req, _res, next) => {
return next(Utils.Response.Success("admin"));
};

export { checkAdmin };
42 changes: 10 additions & 32 deletions src/controllers/event/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ const createEvent: Interfaces.Controller.Async = async (req, res, next) => {
}
}

const connectOrganiser = await Utils.Event.connectId(organizers);

const connectManager = await Utils.Event.connectId(managers);

const event = await prisma.event.create({
data: {
description,
Expand All @@ -137,41 +141,15 @@ const createEvent: Interfaces.Controller.Async = async (req, res, next) => {
module: {
connect: { id: moduleId },
},
organizers: {
create: connectOrganiser,
},
managers: {
create: connectManager,
},
},
});

if (organizers) {
try {
organizers.map(async (id: string) => {
await prisma.eventOrganiser.create({
data: {
userId: id,
eventId: event.id,
},
});
});
} catch (e) {
await prisma.event.delete({ where: { id: event.id } });
return next(Errors.Event.unableToCreate);
}
}

if (managers) {
try {
managers.map(async (id: string) => {
await prisma.eventManager.create({
data: {
userId: id,
eventId: event.id,
},
});
});
} catch (e) {
await prisma.event.delete({ where: { id: event.id } });
return next(Errors.Event.unableToCreate);
}
}

if (!event) return next(Errors.System.serverError);
return res.json(Utils.Response.Success(event));
};
Expand Down
97 changes: 73 additions & 24 deletions src/controllers/event/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,24 @@ const updateEvent: Interfaces.Controller.Async = async (req, res, next) => {

const { eventId: EID } = req.params;
const eventId = String(EID);
if (typeof eventId !== "string" || eventId.length !== 24)
return next(Errors.Module.invalidInput);

const eventOriginal = await prisma.event.findFirst({
where: { id: eventId },
});
if (!eventOriginal) return next(Errors.Module.eventNotFound);
if (moduleId) {
if (!moduleId || typeof moduleId !== "string" || eventId.length !== 24)
return next(Errors.Module.invalidInput);
if (!(await prisma.module.findFirst({ where: { id: moduleId } })))
return next(Errors.Module.moduleNotFound);
}

if (minTeamSize > maxTeamSize) return next(Errors.Module.invalidInput);
if (!String(eventId) || typeof eventId !== "string" || eventId.length !== 24)
return next(Errors.Module.invalidInput);

if (!(await prisma.event.findFirst({ where: { id: eventId } })))
return next(Errors.Module.eventNotFound);

if (!moduleId || typeof moduleId !== "string" || moduleId.length !== 24)
return next(Errors.Module.invalidInput);
if (!(await prisma.module.findFirst({ where: { id: moduleId } })))
return next(Errors.Module.moduleNotFound);

let regStart;
if (registrationStartTime) regStart = new Date(registrationStartTime);
else {
Expand All @@ -57,19 +61,10 @@ const updateEvent: Interfaces.Controller.Async = async (req, res, next) => {
if (regStart && regEnd && regStart > regEnd)
return next(Errors.Module.invalidInput);

const { organizers, managers }: { organizers: [string]; managers: [string] } =
req.body;

let organizersUsernames;
if (organizers) {
organizersUsernames = await Utils.Event.extractUsername(organizers);
if (!organizersUsernames) return next(Errors.User.userNotFound);
}
let managersUsernames;
if (managers) {
managersUsernames = await Utils.Event.extractUsername(managers);
if (!managersUsernames) return next(Errors.User.userNotFound);
}
const {
organizers = [],
managers = [],
}: { organizers: string[]; managers: string[] } = req.body;

if (
(registrationIncentive && !(typeof registrationIncentive === "number")) ||
Expand Down Expand Up @@ -108,6 +103,58 @@ const updateEvent: Interfaces.Controller.Async = async (req, res, next) => {
)
return next(Errors.Module.invalidInput);

const userIdExist = await Utils.Event.userIdExist([
...organizers,
...managers,
]);

if (!userIdExist) {
return next(Errors.User.userNotFound);
}

const eventOrganisers = await prisma.eventOrganiser.findMany({
where: {
eventId: eventId,
},
});

const eventOrganiserIds = eventOrganisers.map(
(organiser) => organiser.userId
);

const organiserIdsToRemove = eventOrganiserIds.filter(
(id) => !organizers.includes(id)
);

const eventManagers = await prisma.eventManager.findMany({
where: {
eventId: eventId,
},
});

const eventManagersUserId = eventManagers.map((manager) => manager.userId);

const managerIdsToRemove = eventManagersUserId.filter(
(id) => !managers.includes(id)
);

const connectOrCreateOrganiser = await Utils.Event.connectOrCreateId(
organizers,
eventId
);

const deleteOrganiser = await Utils.Event.deleteId(
organiserIdsToRemove,
eventId
);

const connectOrCreateManagers = await Utils.Event.connectOrCreateId(
managers,
eventId
);

const deleteManager = await Utils.Event.deleteId(managerIdsToRemove, eventId);

const event = await prisma.event.update({
where: { id: eventId },
data: {
Expand All @@ -126,13 +173,15 @@ const updateEvent: Interfaces.Controller.Async = async (req, res, next) => {
stagesDescription,
venue,
moduleId,
extraQuestions: extraQuestions,
organizers: {
connect: organizersUsernames,
connectOrCreate: connectOrCreateOrganiser,
delete: deleteOrganiser,
},
managers: {
connect: managersUsernames,
connectOrCreate: connectOrCreateManagers,
delete: deleteManager,
},
extraQuestions: extraQuestions,
},
});

Expand Down
3 changes: 3 additions & 0 deletions src/routes/auth.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Router } from "express";

import * as Controllers from "@controllers";
import * as Middlewares from "@middlewares";

const router: Router = Router({ mergeParams: true });

router.post("/signUp", Controllers.Auth.signUp);

router.get("/isAdmin", Middlewares.Auth.isAdmin, Controllers.Auth.checkAdmin);

export default router;
43 changes: 39 additions & 4 deletions src/utils/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ const extractUsername = async (
return userids;
};

const userIdExist = async (organizers: string[]) => {
const userIdExist = async (userIds: string[]) => {
const results = await Promise.all(
organizers.map(async (organizer: string) => {
const user = await prisma.user.findFirst({ where: { id: organizer } });
userIds.map(async (id: string) => {
const user = await prisma.user.findFirst({ where: { id: id } });
if (!user) {
return false;
}
Expand All @@ -34,4 +34,39 @@ const userIdExist = async (organizers: string[]) => {
}
};

export { extractUsername, userIdExist };
const connectId = async (userIds: string[]) => {
const connectResult = userIds.map((id: string) => {
return {
userId: id,
};
});

return connectResult;
};

const connectOrCreateId = async (userIds: string[], eventId: string) => {
const connectOrCreateResult = userIds.map((id: string) => {
return {
create: {
user: { connect: { id: id } },
},
where: {
userId_eventId: { userId: id, eventId: eventId },

Check warning on line 54 in src/utils/event.ts

View workflow job for this annotation

GitHub Actions / lint

Identifier 'userId_eventId' is not in camel case
},
};
});

return connectOrCreateResult;
};

const deleteId = async (userIds: string[], eventId: string) => {
const deleteResult = userIds.map((id: string) => {
return {
userId_eventId: { userId: id, eventId: eventId },

Check warning on line 65 in src/utils/event.ts

View workflow job for this annotation

GitHub Actions / lint

Identifier 'userId_eventId' is not in camel case
};
});

return deleteResult;
};

export { extractUsername, userIdExist, connectId, connectOrCreateId, deleteId };

0 comments on commit c0b308e

Please sign in to comment.