move sendNewsletter flag to communication table
This commit is contained in:
parent
b678067874
commit
46ad96c470
12 changed files with 171 additions and 130 deletions
|
@ -1,6 +1,7 @@
|
|||
export interface CreateCommunicationCommand {
|
||||
preferred: boolean;
|
||||
isSMSAlarming: boolean;
|
||||
isSendNewsletter: boolean;
|
||||
mobile: string;
|
||||
email: string;
|
||||
postalCode: string;
|
||||
|
@ -16,6 +17,7 @@ export interface UpdateCommunicationCommand {
|
|||
id: number;
|
||||
preferred: boolean;
|
||||
isSMSAlarming: boolean;
|
||||
isSendNewsletter: boolean;
|
||||
mobile: string;
|
||||
email: string;
|
||||
postalCode: string;
|
||||
|
|
|
@ -14,26 +14,44 @@ export default abstract class CommunicationCommandHandler {
|
|||
* @returns {Promise<number>}
|
||||
*/
|
||||
static async create(createCommunication: CreateCommunicationCommand): Promise<number> {
|
||||
let insertId = -1;
|
||||
return await dataSource
|
||||
.createQueryBuilder()
|
||||
.insert()
|
||||
.into(communication)
|
||||
.values({
|
||||
preferred: createCommunication.preferred,
|
||||
isSMSAlarming: createCommunication.isSMSAlarming,
|
||||
mobile: createCommunication.mobile,
|
||||
email: createCommunication.email,
|
||||
postalCode: createCommunication.postalCode,
|
||||
city: createCommunication.city,
|
||||
street: createCommunication.street,
|
||||
streetNumber: createCommunication.streetNumber,
|
||||
streetNumberAddition: createCommunication.streetNumberAddition,
|
||||
memberId: createCommunication.memberId,
|
||||
typeId: createCommunication.typeId,
|
||||
.transaction(async (manager) => {
|
||||
await manager
|
||||
.createQueryBuilder()
|
||||
.insert()
|
||||
.into(communication)
|
||||
.values({
|
||||
preferred: createCommunication.preferred,
|
||||
isSMSAlarming: createCommunication.isSMSAlarming,
|
||||
isSendNewsletter: createCommunication.isSendNewsletter,
|
||||
mobile: createCommunication.mobile,
|
||||
email: createCommunication.email,
|
||||
postalCode: createCommunication.postalCode,
|
||||
city: createCommunication.city,
|
||||
street: createCommunication.street,
|
||||
streetNumber: createCommunication.streetNumber,
|
||||
streetNumberAddition: createCommunication.streetNumberAddition,
|
||||
memberId: createCommunication.memberId,
|
||||
typeId: createCommunication.typeId,
|
||||
})
|
||||
.execute()
|
||||
.then((result) => {
|
||||
insertId = result.identifiers[0].id;
|
||||
});
|
||||
|
||||
await manager
|
||||
.createQueryBuilder()
|
||||
.update(communication)
|
||||
.set({
|
||||
isSendNewsletter: false,
|
||||
})
|
||||
.where("memberId = :memberId", { memberId: createCommunication.memberId })
|
||||
.andWhere("id <> :id", { id: insertId })
|
||||
.execute();
|
||||
})
|
||||
.execute()
|
||||
.then((result) => {
|
||||
return result.identifiers[0].id;
|
||||
.then(() => {
|
||||
return insertId;
|
||||
})
|
||||
.catch((err) => {
|
||||
throw new InternalException("Failed creating communication", err);
|
||||
|
@ -47,22 +65,36 @@ export default abstract class CommunicationCommandHandler {
|
|||
*/
|
||||
static async update(updateCommunication: UpdateCommunicationCommand): Promise<void> {
|
||||
return await dataSource
|
||||
.createQueryBuilder()
|
||||
.update(communication)
|
||||
.set({
|
||||
preferred: updateCommunication.preferred,
|
||||
isSMSAlarming: updateCommunication.isSMSAlarming,
|
||||
mobile: updateCommunication.mobile,
|
||||
email: updateCommunication.email,
|
||||
postalCode: updateCommunication.postalCode,
|
||||
city: updateCommunication.city,
|
||||
street: updateCommunication.street,
|
||||
streetNumber: updateCommunication.streetNumber,
|
||||
streetNumberAddition: updateCommunication.streetNumberAddition,
|
||||
.transaction(async (manager) => {
|
||||
await manager
|
||||
.createQueryBuilder()
|
||||
.update(communication)
|
||||
.set({
|
||||
preferred: updateCommunication.preferred,
|
||||
isSMSAlarming: updateCommunication.isSMSAlarming,
|
||||
isSendNewsletter: updateCommunication.isSendNewsletter,
|
||||
mobile: updateCommunication.mobile,
|
||||
email: updateCommunication.email,
|
||||
postalCode: updateCommunication.postalCode,
|
||||
city: updateCommunication.city,
|
||||
street: updateCommunication.street,
|
||||
streetNumber: updateCommunication.streetNumber,
|
||||
streetNumberAddition: updateCommunication.streetNumberAddition,
|
||||
})
|
||||
.where("id = :id", { id: updateCommunication.id })
|
||||
.andWhere("memberId = :memberId", { memberId: updateCommunication.memberId })
|
||||
.execute();
|
||||
|
||||
await manager
|
||||
.createQueryBuilder()
|
||||
.update(communication)
|
||||
.set({
|
||||
isSendNewsletter: false,
|
||||
})
|
||||
.where("memberId = :memberId", { memberId: updateCommunication.memberId })
|
||||
.andWhere("id <> :id", { id: updateCommunication.id })
|
||||
.execute();
|
||||
})
|
||||
.where("id = :id", { id: updateCommunication.id })
|
||||
.andWhere("memberId = :memberId", { memberId: updateCommunication.memberId })
|
||||
.execute()
|
||||
.then(() => {})
|
||||
.catch((err) => {
|
||||
throw new InternalException("Failed updating communication", err);
|
||||
|
|
|
@ -17,11 +17,6 @@ export interface UpdateMemberCommand {
|
|||
internalId?: string;
|
||||
}
|
||||
|
||||
export interface UpdateMemberNewsletterCommand {
|
||||
id: number;
|
||||
communicationId: number;
|
||||
}
|
||||
|
||||
export interface DeleteMemberCommand {
|
||||
id: number;
|
||||
}
|
||||
|
|
|
@ -2,12 +2,7 @@ import { dataSource } from "../../../data-source";
|
|||
import { communication } from "../../../entity/club/member/communication";
|
||||
import { member } from "../../../entity/club/member/member";
|
||||
import InternalException from "../../../exceptions/internalException";
|
||||
import {
|
||||
CreateMemberCommand,
|
||||
DeleteMemberCommand,
|
||||
UpdateMemberCommand,
|
||||
UpdateMemberNewsletterCommand,
|
||||
} from "./memberCommand";
|
||||
import { CreateMemberCommand, DeleteMemberCommand, UpdateMemberCommand } from "./memberCommand";
|
||||
|
||||
export default abstract class MemberCommandHandler {
|
||||
/**
|
||||
|
@ -68,51 +63,6 @@ export default abstract class MemberCommandHandler {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @description update member newsletter
|
||||
* @param {UpdateMemberCommand} updateMember
|
||||
* @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 {number} memberId
|
||||
* @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} deleteMember
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue