93 lines
3.4 KiB
TypeScript
93 lines
3.4 KiB
TypeScript
import { dataSource } from "../data-source";
|
|
import { executivePosition } from "../entity/executivePosition";
|
|
import { memberExecutivePositions } from "../entity/memberExecutivePositions";
|
|
import { user } from "../entity/user";
|
|
import InternalException from "../exceptions/internalException";
|
|
import {
|
|
CreateMemberExecutivePositionCommand,
|
|
DeleteMemberExecutivePositionCommand,
|
|
UpdateMemberExecutivePositionCommand,
|
|
} from "./memberExecutivePositionCommand";
|
|
|
|
export default abstract class MemberExecutivePositionCommandHandler {
|
|
/**
|
|
* @description create memberExecutivePosition
|
|
* @param CreateMemberExecutivePositionCommand
|
|
* @returns {Promise<number>}
|
|
*/
|
|
static async create(createMemberExecutivePosition: CreateMemberExecutivePositionCommand): Promise<number> {
|
|
return await dataSource
|
|
.createQueryBuilder()
|
|
.insert()
|
|
.into(memberExecutivePositions)
|
|
.values({
|
|
note: createMemberExecutivePosition.note,
|
|
start: createMemberExecutivePosition.start,
|
|
end: createMemberExecutivePosition.end,
|
|
member: await dataSource
|
|
.getRepository(user)
|
|
.createQueryBuilder("user")
|
|
.where("id = :id", { id: createMemberExecutivePosition.memberId })
|
|
.getOneOrFail(),
|
|
executivePosition: await dataSource
|
|
.getRepository(executivePosition)
|
|
.createQueryBuilder("executivePosition")
|
|
.where("id = :id", { id: createMemberExecutivePosition.executivePositionId })
|
|
.getOneOrFail(),
|
|
})
|
|
.execute()
|
|
.then((result) => {
|
|
return result.identifiers[0].id;
|
|
})
|
|
.catch((err) => {
|
|
throw new InternalException("Failed creating memberExecutivePosition", err);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @description update memberExecutivePosition
|
|
* @param UpdateMemberExecutivePositionCommand
|
|
* @returns {Promise<void>}
|
|
*/
|
|
static async update(updateMemberExecutivePosition: UpdateMemberExecutivePositionCommand): Promise<void> {
|
|
return await dataSource
|
|
.createQueryBuilder()
|
|
.update(memberExecutivePositions)
|
|
.set({
|
|
note: updateMemberExecutivePosition.note,
|
|
start: updateMemberExecutivePosition.start,
|
|
end: updateMemberExecutivePosition.end,
|
|
executivePosition: await dataSource
|
|
.getRepository(executivePosition)
|
|
.createQueryBuilder("executivePosition")
|
|
.where("id = :id", { id: updateMemberExecutivePosition.executivePositionId })
|
|
.getOneOrFail(),
|
|
})
|
|
.where("id = :id", { id: updateMemberExecutivePosition.id })
|
|
.andWhere("memberId = :memberId", { memberId: updateMemberExecutivePosition.memberId })
|
|
.execute()
|
|
.then(() => {})
|
|
.catch((err) => {
|
|
throw new InternalException("Failed updating memberExecutivePosition", err);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @description delete memberExecutivePosition
|
|
* @param DeleteMemberExecutivePositionCommand
|
|
* @returns {Promise<void>}
|
|
*/
|
|
static async delete(deletMemberExecutivePosition: DeleteMemberExecutivePositionCommand): Promise<void> {
|
|
return await dataSource
|
|
.createQueryBuilder()
|
|
.delete()
|
|
.from(memberExecutivePositions)
|
|
.where("id = :id", { id: deletMemberExecutivePosition.id })
|
|
.andWhere("memberId = :memberId", { memberId: deletMemberExecutivePosition.memberId })
|
|
.execute()
|
|
.then(() => {})
|
|
.catch((err) => {
|
|
throw new InternalException("Failed deleting memberExecutivePosition", err);
|
|
});
|
|
}
|
|
}
|