ff-admin-server/src/command/membershipCommandHandler.ts

108 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-09-27 12:55:34 +00:00
import { dataSource } from "../data-source";
import { membership } from "../entity/membership";
import { membershipStatus } from "../entity/membershipStatus";
import { user } from "../entity/user";
import InternalException from "../exceptions/internalException";
import { CreateMembershipCommand, DeleteMembershipCommand, UpdateMembershipCommand } from "./membershipCommand";
export default abstract class MembershipCommandHandler {
/**
* @description create membership
* @param CreateMembershipCommand
* @returns {Promise<number>}
*/
static async create(createMembership: CreateMembershipCommand): Promise<number> {
let insertid = -1;
return await dataSource
.transaction(async (manager) => {
await manager
.createQueryBuilder()
.insert()
.into(membership)
.values({
internalId: createMembership.internalId,
start: createMembership.start,
member: await dataSource
.getRepository(user)
.createQueryBuilder("user")
.where("id = :id", { id: createMembership.memberId })
.getOneOrFail(),
status: await dataSource
.getRepository(membershipStatus)
.createQueryBuilder("membershipStatus")
.where("id = :id", { id: createMembership.statusId })
.getOneOrFail(),
})
.execute()
.then((result) => {
insertid = result.identifiers[0].id;
});
await manager
.createQueryBuilder()
.update(membership)
.set({
end: createMembership.start,
})
.where("end IS NULL")
.andWhere("memberId = :memberId", { memberId: createMembership.memberId })
.andWhere("id <> :id", { id: insertid })
.execute();
})
.then(() => {
return insertid;
})
.catch((err) => {
throw new InternalException("Failed creating membership", err);
});
}
/**
* @description update membership
* @param UpdateMembershipCommand
* @returns {Promise<void>}
*/
static async update(updateMembership: UpdateMembershipCommand): Promise<void> {
return await dataSource
.createQueryBuilder()
.update(membership)
.set({
internalId: updateMembership.internalId,
start: updateMembership.start,
end: updateMembership.end,
terminationReason: updateMembership.terminationReason,
status: await dataSource
.getRepository(membershipStatus)
.createQueryBuilder("membershipStatus")
.where("id = :id", { id: updateMembership.statusId })
.getOneOrFail(),
})
.where("id = :id", { id: updateMembership.id })
.andWhere("memberId = :memberId", { memberId: updateMembership.memberId })
.execute()
.then(() => {})
.catch((err) => {
throw new InternalException("Failed updating membership", err);
});
}
/**
* @description delete membership
* @param DeleteMembershipCommand
* @returns {Promise<void>}
*/
static async delete(deletMembership: DeleteMembershipCommand): Promise<void> {
return await dataSource
.createQueryBuilder()
.delete()
.from(membership)
.where("id = :id", { id: deletMembership.id })
.andWhere("memberId = :memberId", { memberId: deletMembership.memberId })
.execute()
.then(() => {})
.catch((err) => {
throw new InternalException("Failed deleting membership", err);
});
}
}