2025-01-05 12:12:53 +01:00
|
|
|
import { DeleteResult, EntityManager, InsertResult, UpdateResult } from "typeorm";
|
2025-01-05 14:14:00 +01:00
|
|
|
import { dataSource } from "../../../data-source";
|
|
|
|
import { protocolPresence } from "../../../entity/club/protocol/protocolPresence";
|
|
|
|
import InternalException from "../../../exceptions/internalException";
|
|
|
|
import ProtocolPresenceService from "../../../service/club/protocol/protocolPrecenseService";
|
2025-01-05 12:12:53 +01:00
|
|
|
import { ProtocolPresenceCommand, SynchronizeProtocolPresenceCommand } from "./protocolPresenceCommand";
|
2024-10-13 15:48:01 +02:00
|
|
|
|
|
|
|
export default abstract class ProtocolPresenceCommandHandler {
|
|
|
|
/**
|
|
|
|
* @description sync protocolPresence
|
2025-01-05 14:29:31 +01:00
|
|
|
* @param {SynchronizeProtocolPresenceCommand} syncProtocolPresences
|
2024-10-13 15:48:01 +02:00
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
static async sync(syncProtocolPresences: SynchronizeProtocolPresenceCommand): Promise<void> {
|
2025-01-05 12:12:53 +01:00
|
|
|
let currentPresence = await ProtocolPresenceService.getAll(syncProtocolPresences.protocolId);
|
2024-10-13 15:48:01 +02:00
|
|
|
|
|
|
|
return await dataSource.manager
|
|
|
|
.transaction(async (manager) => {
|
2025-01-05 12:12:53 +01:00
|
|
|
let newMembers = syncProtocolPresences.members.filter(
|
|
|
|
(r) => !currentPresence.some((cp) => cp.memberId == r.memberId)
|
|
|
|
);
|
|
|
|
let removeMembers = currentPresence.filter(
|
|
|
|
(r) => !syncProtocolPresences.members.some((cp) => cp.memberId == r.memberId)
|
|
|
|
);
|
|
|
|
let keptMembers = syncProtocolPresences.members.filter(
|
|
|
|
(m) =>
|
|
|
|
currentPresence.some((cd) => cd.memberId == m.memberId) &&
|
|
|
|
!removeMembers.some((cd) => cd.memberId == m.memberId)
|
|
|
|
);
|
2024-10-13 15:48:01 +02:00
|
|
|
|
2024-10-15 16:25:42 +02:00
|
|
|
if (newMembers.length != 0) {
|
|
|
|
await this.syncPresenceAdd(manager, syncProtocolPresences.protocolId, newMembers);
|
|
|
|
}
|
2024-10-13 15:48:01 +02:00
|
|
|
|
2024-10-15 16:25:42 +02:00
|
|
|
if (removeMembers.length != 0) {
|
|
|
|
await this.syncPresenceRemove(manager, syncProtocolPresences.protocolId, removeMembers);
|
|
|
|
}
|
2025-01-05 12:12:53 +01:00
|
|
|
|
|
|
|
for (const member of keptMembers) {
|
|
|
|
await this.syncPresenceUpdate(manager, syncProtocolPresences.protocolId, member);
|
|
|
|
}
|
2024-10-13 15:48:01 +02:00
|
|
|
})
|
|
|
|
.then(() => {})
|
|
|
|
.catch((err) => {
|
2024-10-15 16:25:42 +02:00
|
|
|
throw new InternalException("Failed saving protocol presence", err);
|
2024-10-13 15:48:01 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private static async syncPresenceAdd(
|
|
|
|
manager: EntityManager,
|
|
|
|
protocolId: number,
|
2025-01-05 12:12:53 +01:00
|
|
|
memberIds: Array<ProtocolPresenceCommand>
|
2024-10-13 15:48:01 +02:00
|
|
|
): Promise<InsertResult> {
|
|
|
|
return await manager
|
|
|
|
.createQueryBuilder()
|
|
|
|
.insert()
|
|
|
|
.into(protocolPresence)
|
|
|
|
.values(
|
|
|
|
memberIds.map((m) => ({
|
2025-01-05 12:12:53 +01:00
|
|
|
...m,
|
2024-10-13 15:48:01 +02:00
|
|
|
protocolId,
|
|
|
|
}))
|
|
|
|
)
|
|
|
|
.execute();
|
|
|
|
}
|
|
|
|
|
2025-01-05 12:12:53 +01:00
|
|
|
private static async syncPresenceUpdate(
|
|
|
|
manager: EntityManager,
|
|
|
|
protocolId: number,
|
|
|
|
member: ProtocolPresenceCommand
|
|
|
|
): Promise<UpdateResult> {
|
|
|
|
return await manager
|
|
|
|
.createQueryBuilder()
|
|
|
|
.update(protocolPresence)
|
|
|
|
.set({
|
|
|
|
absent: member.absent,
|
|
|
|
})
|
|
|
|
.where("memberId = :memberId", { memberId: member.memberId })
|
|
|
|
.andWhere("protocolId = :protocolId", { protocolId })
|
|
|
|
.execute();
|
|
|
|
}
|
|
|
|
|
2024-10-13 15:48:01 +02:00
|
|
|
private static async syncPresenceRemove(
|
|
|
|
manager: EntityManager,
|
|
|
|
protocolId: number,
|
2025-01-05 12:12:53 +01:00
|
|
|
members: Array<ProtocolPresenceCommand>
|
2024-10-13 15:48:01 +02:00
|
|
|
): Promise<DeleteResult> {
|
|
|
|
return await manager
|
|
|
|
.createQueryBuilder()
|
|
|
|
.delete()
|
|
|
|
.from(protocolPresence)
|
2025-01-05 12:12:53 +01:00
|
|
|
.where("memberId IN (:...ids)", { ids: members.map((m) => m.memberId) })
|
2024-10-15 16:25:42 +02:00
|
|
|
.andWhere("protocolId = :protocolId", { protocolId })
|
2024-10-13 15:48:01 +02:00
|
|
|
.execute();
|
|
|
|
}
|
|
|
|
}
|