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

99 lines
3.4 KiB
TypeScript
Raw Normal View History

2024-09-27 12:55:34 +00:00
import { dataSource } from "../data-source";
import { communication } from "../entity/communication";
import { communicationType } from "../entity/communicationType";
import { user } from "../entity/user";
import InternalException from "../exceptions/internalException";
import {
CreateCommunicationCommand,
DeleteCommunicationCommand,
UpdateCommunicationCommand,
} from "./communicationCommand";
export default abstract class CommunicationCommandHandler {
/**
* @description create communication
* @param CreateCommunicationCommand
* @returns {Promise<number>}
*/
static async create(createCommunication: CreateCommunicationCommand): Promise<number> {
return await dataSource
.createQueryBuilder()
.insert()
.into(communication)
.values({
preferred: createCommunication.preferred,
2024-11-27 09:07:59 +00:00
isSMSAlarming: createCommunication.isSMSAlarming,
2024-09-27 12:55:34 +00:00
mobile: createCommunication.mobile,
email: createCommunication.email,
city: createCommunication.city,
street: createCommunication.street,
streetNumber: createCommunication.streetNumber,
streetNumberAddition: createCommunication.streetNumberAddition,
member: await dataSource
.getRepository(user)
.createQueryBuilder("user")
.where("id = :id", { id: createCommunication.memberId })
.getOneOrFail(),
type: await dataSource
.getRepository(communicationType)
.createQueryBuilder("communication")
.where("id = :id", { id: createCommunication.typeId })
.getOneOrFail(),
})
.execute()
.then((result) => {
return result.identifiers[0].id;
})
.catch((err) => {
throw new InternalException("Failed creating communication", err);
});
}
/**
* @description update communication
* @param UpdateCommunicationCommand
* @returns {Promise<void>}
*/
static async update(updateCommunication: UpdateCommunicationCommand): Promise<void> {
return await dataSource
.createQueryBuilder()
.update(communication)
.set({
preferred: updateCommunication.preferred,
2024-11-27 09:07:59 +00:00
isSMSAlarming: updateCommunication.isSMSAlarming,
2024-09-27 12:55:34 +00:00
mobile: updateCommunication.mobile,
email: updateCommunication.email,
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()
.then(() => {})
.catch((err) => {
throw new InternalException("Failed updating communication", err);
});
}
/**
* @description delete communication
* @param DeleteCommunicationCommand
* @returns {Promise<void>}
*/
static async delete(deletCommunication: DeleteCommunicationCommand): Promise<void> {
return await dataSource
.createQueryBuilder()
.delete()
.from(communication)
.where("id = :id", { id: deletCommunication.id })
.andWhere("memberId = :memberId", { memberId: deletCommunication.memberId })
.execute()
.then(() => {})
.catch((err) => {
throw new InternalException("Failed deleting communication", err);
});
}
}