127 lines
3.6 KiB
TypeScript
127 lines
3.6 KiB
TypeScript
import { dataSource } from "../data-source";
|
|
import { communication } from "../entity/communication";
|
|
import { member } from "../entity/member";
|
|
import InternalException from "../exceptions/internalException";
|
|
import CommunicationService from "../service/communicationService";
|
|
import {
|
|
CreateMemberCommand,
|
|
DeleteMemberCommand,
|
|
UpdateMemberCommand,
|
|
UpdateMemberNewsletterCommand,
|
|
} from "./memberCommand";
|
|
|
|
export default abstract class MemberCommandHandler {
|
|
/**
|
|
* @description create member
|
|
* @param CreateMemberCommand
|
|
* @returns {Promise<number>}
|
|
*/
|
|
static async create(createMember: CreateMemberCommand): Promise<number> {
|
|
return await dataSource
|
|
.createQueryBuilder()
|
|
.insert()
|
|
.into(member)
|
|
.values({
|
|
salutation: createMember.salutation,
|
|
firstname: createMember.firstname,
|
|
lastname: createMember.lastname,
|
|
nameaffix: createMember.nameaffix,
|
|
birthdate: createMember.birthdate,
|
|
})
|
|
.execute()
|
|
.then((result) => {
|
|
return result.identifiers[0].id;
|
|
})
|
|
.catch((err) => {
|
|
throw new InternalException("Failed creating member", err);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @description update member
|
|
* @param UpdateMemberCommand
|
|
* @returns {Promise<void>}
|
|
*/
|
|
static async update(updateMember: UpdateMemberCommand): Promise<void> {
|
|
console.log(updateMember);
|
|
return await dataSource
|
|
.createQueryBuilder()
|
|
.update(member)
|
|
.set({
|
|
salutation: updateMember.salutation,
|
|
firstname: updateMember.firstname,
|
|
lastname: updateMember.lastname,
|
|
nameaffix: updateMember.nameaffix,
|
|
birthdate: updateMember.birthdate,
|
|
})
|
|
.where("id = :id", { id: updateMember.id })
|
|
.execute()
|
|
.then(() => {})
|
|
.catch((err) => {
|
|
throw new InternalException("Failed updating member", err);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @description update member newsletter
|
|
* @param UpdateMemberCommand
|
|
* @returns {Promise<void>}
|
|
*/
|
|
static async updateNewsletter(updateMember: UpdateMemberNewsletterCommand): Promise<void> {
|
|
return await dataSource
|
|
.createQueryBuilder()
|
|
.update(member)
|
|
.set({
|
|
sendNewsletter: await dataSource
|
|
.getRepository(communication)
|
|
.createQueryBuilder("communication")
|
|
.where("id = :id", { id: updateMember.communicationId })
|
|
.andWhere("memberId = :memberId", { memberId: updateMember.id })
|
|
.getOneOrFail(),
|
|
})
|
|
.where("id = :id", { id: updateMember.id })
|
|
.execute()
|
|
.then(() => {})
|
|
.catch((err) => {
|
|
throw new InternalException("Failed updating member", err);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @description update member newsletter to unset
|
|
* @param memberId string
|
|
* @returns {Promise<void>}
|
|
*/
|
|
static async unsetNewsletter(memberId: number): Promise<void> {
|
|
return await dataSource
|
|
.createQueryBuilder()
|
|
.update(member)
|
|
.set({
|
|
sendNewsletter: null,
|
|
})
|
|
.where("id = :id", { id: memberId })
|
|
.execute()
|
|
.then(() => {})
|
|
.catch((err) => {
|
|
throw new InternalException("Failed updating member", err);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @description delete member
|
|
* @param DeleteMemberCommand
|
|
* @returns {Promise<void>}
|
|
*/
|
|
static async delete(deleteMember: DeleteMemberCommand): Promise<void> {
|
|
return await dataSource
|
|
.createQueryBuilder()
|
|
.delete()
|
|
.from(member)
|
|
.where("id = :id", { id: deleteMember.id })
|
|
.execute()
|
|
.then(() => {})
|
|
.catch((err) => {
|
|
throw new InternalException("Failed deleting member", err);
|
|
});
|
|
}
|
|
}
|