Skip to content

Commit

Permalink
Merge pull request #1061 from academic-relations/dev
Browse files Browse the repository at this point in the history
Merge dev into main
  • Loading branch information
pbc1017 authored Sep 12, 2024
2 parents e94ca7e + d890812 commit fca232e
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 99 deletions.
109 changes: 61 additions & 48 deletions packages/api/src/drizzle/schema/user.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
int,
mysqlTable,
timestamp,
uniqueIndex,
varchar,
} from "drizzle-orm/mysql-core";

Expand All @@ -28,20 +29,28 @@ export const Student = mysqlTable("student", {
deletedAt: timestamp("deleted_at"),
});

export const StudentT = mysqlTable("student_t", {
id: int("id").autoincrement().primaryKey(),
studentId: int("student_id")
.notNull()
.references(() => Student.id),
studentEnum: int("student_enum").notNull(),
studentStatusEnum: int("student_status_enum").notNull(),
department: int("department"),
semesterId: int("semester_id").notNull(),
startTerm: date("start_term").notNull(),
endTerm: date("end_term"),
createdAt: timestamp("created_at").defaultNow(),
deletedAt: timestamp("deleted_at"),
});
export const StudentT = mysqlTable(
"student_t",
{
id: int("id").autoincrement().primaryKey(),
studentId: int("student_id")
.notNull()
.references(() => Student.id),
studentEnum: int("student_enum").notNull(),
studentStatusEnum: int("student_status_enum").notNull(),
department: int("department"),
semesterId: int("semester_id").notNull(),
startTerm: date("start_term").notNull(),
endTerm: date("end_term"),
createdAt: timestamp("created_at").defaultNow(),
deletedAt: timestamp("deleted_at"),
},
table => ({
StduentTStudentIdSemesterIdUniqueKey: uniqueIndex(
"student_t_student_id_semester_id_unique_key",
).on(table.studentId, table.semesterId),
}),
);

export const Executive = mysqlTable("executive", {
id: int("id").autoincrement().primaryKey(),
Expand All @@ -68,22 +77,30 @@ export const ExecutiveBureauEnum = mysqlTable("executive_bureau_enum", {
name: varchar("name", { length: 31 }),
});

export const ExecutiveT = mysqlTable("executive_t", {
id: int("id").autoincrement().primaryKey(),
executiveId: int("executive_id")
.notNull()
.references(() => Executive.id),
executiveStatusEnum: int("executive_status_enum")
.notNull()
.references(() => ExecutiveStatusEnum.id),
executiveBureauEnum: int("executive_bureau_enum")
.notNull()
.references(() => ExecutiveBureauEnum.id),
startTerm: date("start_term").notNull(),
endTerm: date("end_term"),
createdAt: timestamp("created_at").defaultNow(),
deletedAt: timestamp("deleted_at"),
});
export const ExecutiveT = mysqlTable(
"executive_t",
{
id: int("id").autoincrement().primaryKey(),
executiveId: int("executive_id")
.notNull()
.references(() => Executive.id),
executiveStatusEnum: int("executive_status_enum")
.notNull()
.references(() => ExecutiveStatusEnum.id),
executiveBureauEnum: int("executive_bureau_enum")
.notNull()
.references(() => ExecutiveBureauEnum.id),
startTerm: date("start_term").notNull(),
endTerm: date("end_term"),
createdAt: timestamp("created_at").defaultNow(),
deletedAt: timestamp("deleted_at"),
},
table => ({
StduentTStudentIdSemesterIdUniqueKey: uniqueIndex(
"executive_t_executive_id_start_term_unique_key",
).on(table.executiveId, table.startTerm),
}),
);

export const Professor = mysqlTable("professor", {
id: int("id").autoincrement().primaryKey(),
Expand All @@ -95,24 +112,19 @@ export const Professor = mysqlTable("professor", {
deletedAt: timestamp("deleted_at"),
});

export const ProfessorT = mysqlTable(
"professor_t",
{
id: int("id").autoincrement().primaryKey(),
professorId: int("professor_id")
.notNull()
.references(() => Professor.id),
professorEnum: int("professor_enum"),
department: int("department"),
startTerm: date("start_term").notNull(),
endTerm: date("end_term"),
createdAt: timestamp("created_at").defaultNow(),
deletedAt: timestamp("deleted_at"),
},
// t => ({
// unq1: unique().on(t.professorId, t.professorEnum, t.department, t.startTerm),
// }),
);
export const ProfessorT = mysqlTable("professor_t", {
id: int("id").autoincrement().primaryKey(),
professorId: int("professor_id")
.notNull()
.unique()
.references(() => Professor.id),
professorEnum: int("professor_enum"),
department: int("department"),
startTerm: date("start_term").notNull(),
endTerm: date("end_term"),
createdAt: timestamp("created_at").defaultNow(),
deletedAt: timestamp("deleted_at"),
});

export const Employee = mysqlTable("employee", {
id: int("id").autoincrement().primaryKey(),
Expand All @@ -131,6 +143,7 @@ export const EmployeeT = mysqlTable(
id: int("id").autoincrement().primaryKey(),
employeeId: int("employee_id")
.notNull()
.unique()
.references(() => Employee.id),
startTerm: date("start_term").notNull(),
endTerm: date("end_term"),
Expand Down
28 changes: 14 additions & 14 deletions packages/api/src/feature/auth/repository/auth.repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Inject, Injectable } from "@nestjs/common";
import { and, eq, gte, lte } from "drizzle-orm";
import { and, eq, gte, isNull, lte, or } from "drizzle-orm";
import { MySql2Database } from "drizzle-orm/mysql2";

import { getKSTDate, takeUnique } from "@sparcs-clubs/api/common/util/util";
Expand Down Expand Up @@ -166,7 +166,13 @@ export class AuthRepository {
startTerm: semester.startTerm,
endTerm: semester.endTerm,
})
.onDuplicateKeyUpdate({ set: { department: parseInt(department) } });
.onDuplicateKeyUpdate({
set: {
studentEnum,
studentStatusEnum,
department: parseInt(department),
},
});

// type이 "Student"인 경우 executive table에서 해당 studentNumber이 있는지 확인
// 있으면 해당 칼럼의 user_id를 업데이트
Expand All @@ -183,8 +189,11 @@ export class AuthRepository {
ExecutiveT,
and(
eq(ExecutiveT.executiveId, Executive.id),
gte(ExecutiveT.endTerm, currentDate),
lte(ExecutiveT.startTerm, currentDate),
or(
gte(ExecutiveT.endTerm, currentDate),
isNull(ExecutiveT.endTerm),
),
),
)
.where(eq(Executive.studentId, student.id))
Expand All @@ -196,11 +205,7 @@ export class AuthRepository {
};
}
}
// TODO
// type이 "Teacher"를 포함하는 경우 professor table에서 해당 email이 있는지 확인 후 upsert
// professor_t에서 해당 professor_id이 있는지 확인 후 upsert
// type이 "Employee"를 포함하는 경우 Employee table에서 해당 email이 있는지 확인 후 upsert
// employee_t에서 해당 employee_id이 있는지 확인 후 upsert

if (type.includes("Teacher")) {
await this.db
.insert(Professor)
Expand All @@ -216,17 +221,14 @@ export class AuthRepository {
.values({
department: parseInt(department),
professorId: professor.id,
professorEnum: 1,
professorEnum: 3,
startTerm: semester.startTerm,
endTerm: semester.endTerm,
})
.onDuplicateKeyUpdate({
set: {
department: parseInt(department),
professorId: professor.id,
professorEnum: 1,
startTerm: semester.startTerm,
endTerm: semester.endTerm,
},
});
result.professor = {
Expand Down Expand Up @@ -259,13 +261,11 @@ export class AuthRepository {
.values({
employeeId: employee.id,
startTerm: semester.startTerm,
endTerm: semester.endTerm,
})
.onDuplicateKeyUpdate({
set: {
employeeId: employee.id,
startTerm: semester.startTerm,
endTerm: semester.endTerm,
},
});
result.employee = {
Expand Down
19 changes: 6 additions & 13 deletions packages/api/src/feature/auth/service/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,12 @@ export class AuthService {

const ssoProfile: SSOUser = await this.ssoClient.get_user_info(query.code);

let studentNumber =
ssoProfile.kaist_info.ku_std_no || process.env.USER_KU_STD_NO;
let email = ssoProfile.email || process.env.USER_MAIL;
let sid = ssoProfile.sid || process.env.USER_SID;
let name = ssoProfile.kaist_info.ku_kname || process.env.USER_KU_KNAME;
let type =
ssoProfile.kaist_info.ku_person_type || process.env.USER_KU_PERSON_TYPE;
let department =
ssoProfile.kaist_info.ku_kaist_org_id || process.env.USER_KU_KAIST_ORG_ID;
let studentNumber = ssoProfile.kaist_info.ku_std_no || "00000000";
let email = ssoProfile.email || "[email protected]";
let sid = ssoProfile.sid || "00000000";
let name = ssoProfile.kaist_info.ku_kname || "unknown";
let type = ssoProfile.kaist_info.ku_person_type || "Student";
let department = ssoProfile.kaist_info.ku_kaist_org_id || "4421";

if (process.env.NODE_ENV === "local") {
studentNumber = process.env.USER_KU_STD_NO;
Expand All @@ -68,10 +65,6 @@ export class AuthService {
department = process.env.USER_KU_KAIST_ORG_ID;
}

if (!studentNumber || !email || !sid || !name || !type || !department) {
throw new HttpException("Invalid SSO Profile", 403);
}

const user = await this.authRepository.findOrCreateUser(
email,
studentNumber,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,15 @@ export class ClubRegistrationRepository {
.for("share")
.then(takeUnique);
if (!professor) {
professor = await tx.insert(Professor).values({
email: body.professor.email,
name: body.professor.name,
});
professor = await tx
.insert(Professor)
.values({
email: body.professor.email,
name: body.professor.name,
})
.onDuplicateKeyUpdate({
set: { name: body.professor.name },
});
professor = await tx
.select({
id: Professor.id,
Expand All @@ -179,11 +184,16 @@ export class ClubRegistrationRepository {

logger.debug(professor);

await tx.insert(ProfessorT).values({
professorId: professor.id,
professorEnum: body.professor.professorEnumId,
startTerm: cur,
});
await tx
.insert(ProfessorT)
.values({
professorId: professor.id,
professorEnum: body.professor.professorEnumId,
startTerm: cur,
})
.onDuplicateKeyUpdate({
set: { professorEnum: body.professor.professorEnumId },
});
}
}

Expand Down Expand Up @@ -276,10 +286,15 @@ export class ClubRegistrationRepository {
.for("share")
.then(takeUnique);
if (!professor) {
professor = await tx.insert(Professor).values({
email: body.professor.email,
name: body.professor.name,
});
professor = await tx
.insert(Professor)
.values({
email: body.professor.email,
name: body.professor.name,
})
.onDuplicateKeyUpdate({
set: { name: body.professor.name },
});
professor = await tx
.select({
id: Professor.id,
Expand All @@ -297,11 +312,16 @@ export class ClubRegistrationRepository {

logger.debug(professor);

await tx.insert(ProfessorT).values({
professorId: professor.id,
professorEnum: body.professor.professorEnumId,
startTerm: cur,
});
await tx
.insert(ProfessorT)
.values({
professorId: professor.id,
professorEnum: body.professor.professorEnumId,
startTerm: cur,
})
.onDuplicateKeyUpdate({
set: { professorEnum: body.professor.professorEnumId },
});
}
}

Expand Down Expand Up @@ -406,7 +426,7 @@ export class ClubRegistrationRepository {
and(
eq(Student.id, StudentT.studentId),
lte(StudentT.startTerm, cur),
or(gt(StudentT.endTerm, cur), isNull(StudentT.endTerm)),
or(gte(StudentT.endTerm, cur), isNull(StudentT.endTerm)),
isNull(StudentT.deletedAt),
),
)
Expand Down Expand Up @@ -494,11 +514,7 @@ export class ClubRegistrationRepository {
),
)
.where(
and(
eq(Registration.id, applyId),
eq(Registration.studentId, studentId),
isNull(Registration.deletedAt),
),
and(eq(Registration.id, applyId), isNull(Registration.deletedAt)),
)
.for("share")
.then(takeUnique);
Expand Down

0 comments on commit fca232e

Please sign in to comment.