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

109 lines
3.1 KiB
TypeScript
Raw Normal View History

2024-09-26 11:16:43 +00:00
import { dataSource } from "../data-source";
2024-09-27 12:55:34 +00:00
import { communication } from "../entity/communication";
2024-09-26 11:16:43 +00:00
import { member } from "../entity/member";
import InternalException from "../exceptions/internalException";
2024-09-27 12:55:34 +00:00
import CommunicationService from "../service/communicationService";
import {
CreateMemberCommand,
DeleteMemberCommand,
UpdateMemberCommand,
UpdateMemberNewsletterCommand,
} from "./memberCommand";
2024-09-26 11:16:43 +00:00
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);
});
}
2024-09-27 12:55:34 +00:00
/**
* @description update member newsletter
* @param UpdateMemberCommand
* @returns {Promise<void>}
*/
static async updateNewsletter(updateMember: UpdateMemberNewsletterCommand): Promise<void> {
console.log(updateMember);
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);
});
}
2024-09-26 11:16:43 +00:00
/**
* @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);
});
}
}