import { dataSource } from "../data-source"; import { qualification } from "../entity/qualification"; import { memberQualifications } from "../entity/memberQualifications"; import { user } from "../entity/user"; import InternalException from "../exceptions/internalException"; import { CreateMemberQualificationCommand, DeleteMemberQualificationCommand, UpdateMemberQualificationCommand, } from "./memberQualificationCommand"; export default abstract class MemberQualificationCommandHandler { /** * @description create memberQualification * @param CreateMemberQualificationCommand * @returns {Promise} */ static async create(createMemberQualification: CreateMemberQualificationCommand): Promise { return await dataSource .createQueryBuilder() .insert() .into(memberQualifications) .values({ note: createMemberQualification.note, start: createMemberQualification.start, member: await dataSource .getRepository(user) .createQueryBuilder("user") .where("id = :id", { id: createMemberQualification.memberId }) .getOneOrFail(), qualification: await dataSource .getRepository(qualification) .createQueryBuilder("qualification") .where("id = :id", { id: createMemberQualification.qualificationId }) .getOneOrFail(), }) .execute() .then((result) => { return result.identifiers[0].id; }) .catch((err) => { throw new InternalException("Failed creating memberQualification", err); }); } /** * @description update memberQualification * @param UpdateMemberQualificationCommand * @returns {Promise} */ static async update(updateMemberQualification: UpdateMemberQualificationCommand): Promise { return await dataSource .createQueryBuilder() .update(memberQualifications) .set({ note: updateMemberQualification.note, start: updateMemberQualification.start, end: updateMemberQualification.end, terminationReason: updateMemberQualification.terminationReason, qualification: await dataSource .getRepository(qualification) .createQueryBuilder("qualification") .where("id = :id", { id: updateMemberQualification.qualificationId }) .getOneOrFail(), }) .where("id = :id", { id: updateMemberQualification.id }) .andWhere("memberId = :memberId", { memberId: updateMemberQualification.memberId }) .execute() .then(() => {}) .catch((err) => { throw new InternalException("Failed updating memberQualification", err); }); } /** * @description delete memberQualification * @param DeleteMemberQualificationCommand * @returns {Promise} */ static async delete(deletMemberQualification: DeleteMemberQualificationCommand): Promise { return await dataSource .createQueryBuilder() .delete() .from(memberQualifications) .where("id = :id", { id: deletMemberQualification.id }) .andWhere("memberId = :memberId", { memberId: deletMemberQualification.memberId }) .execute() .then(() => {}) .catch((err) => { throw new InternalException("Failed deleting memberQualification", err); }); } }