Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user ID to timetables in search service #211

Merged
merged 11 commits into from
Jul 29, 2024
23 changes: 2 additions & 21 deletions backend/src/controllers/timetable/deleteTimetable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import "dotenv/config";
import { Request, Response } from "express";
import { z } from "zod";
import { timetableIDType } from "../../../../lib/src/index.js";
import { env } from "../../config/server.js";
import { Timetable, User } from "../../entity/entities.js";
import { validate } from "../../middleware/zodValidateRequest.js";
import { timetableRepository } from "../../repositories/timetableRepository.js";
import { userRepository } from "../../repositories/userRepository.js";
import { removeTimetable } from "../../utils/search.js";
import sqids, { validSqid } from "../../utils/sqids.js";

const dataSchema = z.object({
Expand Down Expand Up @@ -74,27 +74,8 @@ export const deleteTimetable = async (req: Request, res: Response) => {
res.status(500).json({ message: "Internal Server Error" });
}
try {
const searchServiceURL = `${env.SEARCH_SERVICE_URL}/timetable/remove`;

const res = await fetch(searchServiceURL, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ id: req.params.id }),
});
if (!res.ok) {
const resJson = await res.json();
logger.error(
"Error while removing timetable from search service: ",
resJson.error,
);
}
await removeTimetable(timetable.id, logger);
} catch (err: any) {
logger.error(
"Error while removing timetable from search service: ",
err.message,
);
return res.status(500).json({ message: "Internal Server Error" });
}
return res.json({ message: "timetable deleted" });
Expand Down
62 changes: 9 additions & 53 deletions backend/src/controllers/timetable/editTimetableMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {
namedNonEmptyStringType,
timetableIDType,
} from "../../../../lib/src/index.js";
import { env } from "../../config/server.js";
import { Timetable, User } from "../../entity/entities.js";
import { validate } from "../../middleware/zodValidateRequest.js";
import { timetableRepository } from "../../repositories/timetableRepository.js";
import { userRepository } from "../../repositories/userRepository.js";
import { addTimetable, removeTimetable } from "../../utils/search.js";
import sqids, { validSqid } from "../../utils/sqids.js";

const dataSchema = z.object({
Expand Down Expand Up @@ -117,59 +117,15 @@ export const editTimetableMetadata = async (req: Request, res: Response) => {
return res.status(500).json({ message: "Internal Server Error" });
}

if (isDraft === false && isPrivate === false) {
try {
const searchServiceURL = `${env.SEARCH_SERVICE_URL}/timetable/add`;
const updatedTimetableStringID = {
...updatedTimetable,
id: req.params.id,
};
const res = await fetch(searchServiceURL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(updatedTimetableStringID),
});
const resJson = await res.json();
if (!res.ok) {
logger.error(
"Error while adding timetable to search service: ",
resJson.error,
);
}
} catch (err: any) {
logger.error(
"Error while adding timetable to search service: ",
err.message,
);
return res.status(500).json({ message: "Internal Server Error" });
}
} else {
try {
const searchServiceURL = `${env.SEARCH_SERVICE_URL}/timetable/remove`;

const res = await fetch(searchServiceURL, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ id: req.params.id }),
});
if (!res.ok) {
const resJson = await res.json();
logger.error(
"Error while removing timetable from search service: ",
resJson.error,
);
}
} catch (err: any) {
logger.error(
"Error while removing timetable from search service: ",
err.message,
);
return res.status(500).json({ message: "Internal Server Error" });
// update search service
try {
if (isDraft === false && isPrivate === false) {
await addTimetable(updatedTimetable, req.session?.email ?? "", logger);
} else {
await removeTimetable(timetable.id, logger);
}
} catch (err: any) {
return res.status(500).json({ message: "Internal Server Error" });
}

return res.json({ message: "timetable edited" });
Expand Down
103 changes: 12 additions & 91 deletions backend/src/controllers/timetable/updateChangedTimetable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import {
checkForClassHoursClash,
checkForExamHoursClash,
} from "../../utils/checkForClashes.js";
import sqids from "../../utils/sqids.js";
import {
addCourse,
addTimetable,
removeCourse,
removeTimetable,
} from "../../utils/search.js";
import { addExamTimings, removeSection } from "../../utils/updateSection.js";
import { updateSectionWarnings } from "../../utils/updateWarnings.js";

Expand Down Expand Up @@ -234,112 +239,28 @@ export const updateChangedTimetable = async (req: Request, res: Response) => {

// update course in search service
try {
const searchServiceURL = `${env.SEARCH_SERVICE_URL}/course/remove`;
const res = await fetch(searchServiceURL, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ id: course.id }),
});
if (!res.ok) {
const resJson = await res.json();
logger.error(
"Error while removing course from search service: ",
resJson.error,
);
}
await removeCourse(course.id, logger);
await addCourse(course, logger);
} catch (err: any) {
logger.error(
"Error while removing course from search service: ",
err.message,
);
return res.status(500).json({ message: "Internal Server Error" });
}

try {
const searchServiceURL = `${env.SEARCH_SERVICE_URL}/course/add`;
const res = await fetch(searchServiceURL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(course),
});
if (!res.ok) {
const resJson = await res.json();
logger.error(
"Error while adding course to search service: ",
resJson.error,
);
}
} catch (err: any) {
logger.error(
"Error while adding course to search service: ",
err.message,
);
return res.status(500).json({ message: "Internal Server Error" });
}

// update timetables in search service
for (const timetable of timetables) {
try {
const searchServiceURL = `${env.SEARCH_SERVICE_URL}/timetable/remove`;
const encodedId = sqids.encode([timetable.id]);
const res = await fetch(searchServiceURL, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ id: encodedId }),
});

if (!res.ok) {
const resJson = await res.json();
logger.error(
"Error while removing timetable from search service: ",
resJson.error,
);
}
await removeTimetable(timetable.id, logger);
} catch (err: any) {
logger.error(
"Error while removing timetable from search service: ",
err.message,
);
return res.status(500).json({ message: "Internal Server Error" });
}
if (!timetable.draft && !timetable.private) {
const timetableWithSections = await timetableRepository
.createQueryBuilder("timetable")
.leftJoinAndSelect("timetable.sections", "section")
.where("timetable.id=:id", { id: timetable.id })
.getOne();
const encodedId = sqids.encode([timetable.id]);
const timetableWithSectionsString = {
...timetableWithSections,
id: encodedId,
};
.where("timetable.id = :id", { id: timetable.id })
.getOneOrFail();
try {
const searchServiceURL = `${env.SEARCH_SERVICE_URL}/timetable/add`;
const res = await fetch(searchServiceURL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(timetableWithSectionsString),
});
if (!res.ok) {
const resJson = await res.json();
logger.error(
"Error while adding timetable to search service: ",
resJson.error,
);
}
await addTimetable(timetableWithSections, null, logger);
} catch (err: any) {
logger.error(
"Error while adding timetable to search service: ",
err.message,
);
return res.status(500).json({ message: "Internal Server Error" });
}
}
Expand Down
Loading
Loading