Skip to content

Commit

Permalink
refactor: new api from sparcs-ar
Browse files Browse the repository at this point in the history
  • Loading branch information
pbc1017 committed Mar 17, 2024
1 parent 9dc43ac commit 42a071e
Showing 1 changed file with 85 additions and 91 deletions.
176 changes: 85 additions & 91 deletions back/routes/new/clubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ const {
RegistrationMember,
} = require("../../models");

router.get("/my", async (req, res) => {
const student_id = 20210227; // Hardcoded student ID as per instructions

router.get("/", async (req, res) => {
try {
// Get the current semester
const currentDate = new Date();
Expand All @@ -35,76 +33,50 @@ router.get("/my", async (req, res) => {
.json({ success: false, message: "No current semester found" });
}

// Fetch all semesters where the student was active
const semesters = await MemberClub.findAll({
where: { student_id },
attributes: ["semester_id"],
// Fetch all divisions except the ones with ids 13 and 14
const divisions = await Division.findAll({
attributes: ["id", "name"],
where: {
id: { [Op.notIn]: [13, 14] },
},
include: [
{
model: Semester,
as: "semesters",
attributes: ["id", "year", "semester"],
},
],
group: ["semester_id"],
order: [
[{ model: Semester, as: "semesters" }, "year", "DESC"],
[{ model: Semester, as: "semesters" }, "semester", "ASC"],
],
});

const semesterDetails = await Promise.all(
semesters.map(async (item) => {
// Fetch clubs that the student was part of in each semester
const memberClubs = await MemberClub.findAll({
where: {
student_id: student_id,
semester_id: item.semester_id,
},
attributes: ["club_id"],
});

const clubIds = memberClubs.map((mc) => mc.club_id);

// Fetch club details for these clubs in the current semester
const clubs = await Club.findAll({
where: { id: { [Op.in]: clubIds } },
model: Club,
as: "Clubs",
attributes: ["id", "name"],
include: [
{
model: SemesterClub,
as: "SemesterClubs",
where: { semester_id: currentSemester.id },
attributes: ["type_id", "characteristic_kr", "advisor"],
required: true,
where: {
semester_id: currentSemester.id,
},
include: [
{
model: SemesterClubType,
as: "type",
attributes: ["type"],
attributes: ["type"], // Assuming 'type' is a field in SemesterClubType
},
],
attributes: ["type_id", "characteristic_kr", "advisor"],
},
],
attributes: ["id", "name"],
});

// Format club details with representative and total members
const formattedClubs = await Promise.all(
clubs.map(async (club) => {
// Fetch representative name
const representative = await ClubRepresentative.findOne({
where: {
club_id: club.id,
type_id: 1, // Assuming type_id 1 is for the president or main representative
[Op.or]: [
{ end_term: { [Op.gte]: currentDate } },
{ end_term: null },
],
},
include: [{ model: Member, as: "student", attributes: ["name"] }],
});
},
],
});

// Count total members for the club in the current semester
const totalMembersCount = await MemberClub.count({
const divisionClubsData = await Promise.all(
divisions.map(async (division) => {
const clubsData = await Promise.all(
division.Clubs.map(async (club) => {
// Get representative, type, and member count details
const clubType = club.SemesterClubs[0].type.type;
const representative = await getClubRepresentative(
club.id,
currentDate
);
const totalMembers = await MemberClub.count({
where: {
club_id: club.id,
semester_id: currentSemester.id,
Expand All @@ -114,29 +86,30 @@ router.get("/my", async (req, res) => {
return {
id: club.id,
name: club.name,
type: club.SemesterClubs[0]?.type?.type || "N/A", // Handling possible undefined values
characteristic: club.SemesterClubs[0]?.characteristic_kr || "N/A",
representative: representative
? representative.student.name
: "N/A",
advisor: club.SemesterClubs[0]?.advisor || "N/A",
totalMembers: totalMembersCount,
type: clubType,
characteristic: club.SemesterClubs[0].characteristic_kr,
representative: representative,
advisor: club.SemesterClubs[0].advisor,
totalMembers: totalMembers,
};
})
);

return {
semester: item.semester_id,
name: `${item.semesters.year} ${item.semesters.semester}`,
clubs: formattedClubs,
division: division.id,
name: division.name,
clubs: clubsData,
};
})
);

res.json(semesterDetails);
res.json(divisionClubsData);
} catch (error) {
console.error(error);
res.status(500).json({ success: false, message: "Internal Server Error" });
res.status(500).json({
success: false,
message: "Internal Server Error",
});
}
});

Expand Down Expand Up @@ -309,22 +282,20 @@ router.get("/my", async (req, res) => {
],
});

// console.log(semesters[0].semesters);
const semesterDetails = await Promise.all(
semesters.map(async (item) => {
// Fetch clubs that the student was part of in the current semester
// Fetch clubs that the student was part of in each semester
const memberClubs = await MemberClub.findAll({
where: {
student_id: student_id,
semester_id: item.semesters.id, // Use current semester's ID
semester_id: item.semester_id,
},
attributes: ["club_id"],
});

const clubIds = memberClubs.map((mc) => mc.club_id);
console.log(clubIds);

// Fetch current semester details for these clubs
// Fetch club details for these clubs in the current semester
const clubs = await Club.findAll({
where: { id: { [Op.in]: clubIds } },
include: [
Expand All @@ -344,18 +315,44 @@ router.get("/my", async (req, res) => {
],
attributes: ["id", "name"],
});
console.log(clubs);

// Format club details
const formattedClubs = clubs.map((club) => ({
id: club.id,
name: club.name,
type: club.SemesterClubs[0].type.type,
characteristic: club.SemesterClubs[0].characteristic_kr,
representative: "", // This would require an additional query to fetch based on current semester
advisor: club.SemesterClubs[0].advisor,
totalMembers: 0, // This requires another query to count members for each club
}));

// Format club details with representative and total members
const formattedClubs = await Promise.all(
clubs.map(async (club) => {
// Fetch representative name
const representative = await ClubRepresentative.findOne({
where: {
club_id: club.id,
type_id: 1, // Assuming type_id 1 is for the president or main representative
[Op.or]: [
{ end_term: { [Op.gte]: currentDate } },
{ end_term: null },
],
},
include: [{ model: Member, as: "student", attributes: ["name"] }],
});

// Count total members for the club in the current semester
const totalMembersCount = await MemberClub.count({
where: {
club_id: club.id,
semester_id: currentSemester.id,
},
});

return {
id: club.id,
name: club.name,
type: club.SemesterClubs[0]?.type?.type || "N/A", // Handling possible undefined values
characteristic: club.SemesterClubs[0]?.characteristic_kr || "N/A",
representative: representative
? representative.student.name
: "N/A",
advisor: club.SemesterClubs[0]?.advisor || "N/A",
totalMembers: totalMembersCount,
};
})
);

return {
semester: item.semester_id,
Expand All @@ -368,10 +365,7 @@ router.get("/my", async (req, res) => {
res.json(semesterDetails);
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Internal Server Error",
});
res.status(500).json({ success: false, message: "Internal Server Error" });
}
});

Expand Down

0 comments on commit 42a071e

Please sign in to comment.