member communication

This commit is contained in:
Anton Schegg 2025-01-03 18:50:23 +01:00
parent c8b45512c5
commit 2398084764

View file

@ -16,9 +16,26 @@ import { CreateExecutivePositionCommand, DeleteExecutivePositionCommand } from "
import ExecutivePositionCommandHandler from "../command/executivePositionCommandHandler"; import ExecutivePositionCommandHandler from "../command/executivePositionCommandHandler";
import ExecutivePositionService from "../service/executivePositionService"; import ExecutivePositionService from "../service/executivePositionService";
import MembershipStatusService from "../service/membershipStatusService"; import MembershipStatusService from "../service/membershipStatusService";
import { CreateMembershipCommand, UpdateMembershipCommand } from "../command/membershipCommand"; import {
CreateMembershipCommand,
DeleteMembershipCommand,
UpdateMembershipCommand,
} from "../command/membershipCommand";
import MembershipCommandHandler from "../command/membershipCommandHandler"; import MembershipCommandHandler from "../command/membershipCommandHandler";
import { updateMembershipOfMember } from "../controller/admin/memberController"; import MembershipService from "../service/membershipService";
import {
CreateMemberExecutivePositionCommand,
DeleteMemberExecutivePositionCommand,
} from "../command/memberExecutivePositionCommand";
import MemberExecutivePositionCommandHandler from "../command/memberExecutivePositionCommandHandler";
import { CreateCommunicationTypeCommand, DeleteCommunicationTypeCommand } from "../command/communicationTypeCommand";
import CommunicationTypeService from "../service/communicationTypeService";
import CommunicationTypeCommandHandler from "../command/communicationTypeCommandHandler";
import CommunicationService from "../service/communicationService";
import { CreateCommunicationCommand, DeleteCommunicationCommand } from "../command/communicationCommand";
import CommunicationCommandHandler from "../command/communicationCommandHandler";
import MemberExecutivePositionService from "../service/memberExecutivePositionService";
import { communication } from "../entity/communication";
var router = express.Router({ mergeParams: true }); var router = express.Router({ mergeParams: true });
@ -56,17 +73,47 @@ router.get("/", async (req, res) => {
// Delete members // Delete members
let [members, total] = await MemberService.getAll(0, 9999); let [members, total] = await MemberService.getAll(0, 9999);
for (const m of members) { for (const m of members) {
// delete all member awards of this member // delete all member awards of this member
let memberAwards = await MemberAwardService.getAll(m.id); let memberAwards = await MemberAwardService.getAll(m.id);
for (const award of memberAwards) { for (const award of memberAwards) {
let deleteMemberAward: DeleteMemberAwardCommand = { let deleteMemberAward: DeleteMemberAwardCommand = {
id: award.id, memberId: m.id id: award.id, memberId: m.id
}; };
console.log(`Deleting award ${award.id} from member ${m.id}`); console.log(`Deleting award ${award.id} from ${m.firstname} ${m.lastname}`);
await MemberAwardCommandHandler.delete(deleteMemberAward) await MemberAwardCommandHandler.delete(deleteMemberAward)
} }
// Delete communication from this member
let communications = await CommunicationService.getAll(m.id);
for (const comm of communications) {
let deleteCommunication: DeleteCommunicationCommand = {
id: comm.id, memberId: 0
};
console.log(`Delete communication ${comm.type} from ${m.firstname} ${m.lastname}`);
await CommunicationCommandHandler.delete(deleteCommunication);
}
// Delete membership entries
let memberships = await MembershipService.getAll(m.id);
for (const membership of memberships) {
let deleteMembership: DeleteMembershipCommand = {
id: membership.id, memberId: m.id
};
console.log(`Delete membership ${membership.id} from ${m.firstname} ${m.lastname}`);
await MembershipCommandHandler.delete(deleteMembership);
}
// Delete membership positions entries
let memberExecutivePositions = await MemberExecutivePositionService.getAll(m.id);
for (const memberPos of memberExecutivePositions) {
let deleteMemberExecutivePosition: DeleteMemberExecutivePositionCommand = {
id: memberPos.id, memberId: m.id
};
console.log(`Delete position ${deleteMemberExecutivePosition.id} from ${m.firstname} ${m.lastname}`);
await MemberExecutivePositionCommandHandler.delete(deleteMemberExecutivePosition);
}
// delete member itself
let deleteMember: DeleteMemberCommand = { let deleteMember: DeleteMemberCommand = {
id: m.id, id: m.id,
}; };
@ -84,19 +131,14 @@ router.get("/", async (req, res) => {
await AwardCommandHandler.delete(deleteAward); await AwardCommandHandler.delete(deleteAward);
} }
const sAwardsById :any = {}; // Delete communication types
const sAwardsByName :any = {}; let commTypes = await CommunicationTypeService.getAll();
for (const commType of commTypes) {
// Create Awards let deleteCommunicationType: DeleteCommunicationTypeCommand = {
const awardNames = ['Ehrennadel Silber', 'Ehrennadel Gold', 'Ehrenkreuz Silber', 'Ehrenkreuz Gold', 'Ehrenmitgliedschaft', 'Ehrung 25 Jahre aktiv', 'Ehrung 40 Jahre aktiv']; id: commType.id,
for (const awardName of awardNames) {
let createAward: CreateAwardCommand = {
award: awardName,
}; };
const awardId = await AwardCommandHandler.create(createAward); console.log(`Delete communication type ${commType.type}`);
console.log(`Created award ${awardName} with id ${awardId}`); await CommunicationTypeCommandHandler.delete(deleteCommunicationType);
sAwardsById[awardId] = awardName;
sAwardsByName[awardName] = awardId;
} }
// Delete postions // Delete postions
@ -109,17 +151,7 @@ router.get("/", async (req, res) => {
await ExecutivePositionCommandHandler.delete(deletePosition); await ExecutivePositionCommandHandler.delete(deletePosition);
} }
// Create positions // Delete membership states
let positionsResult: any[] = await db('Funktionen');
for (const position of positionsResult) {
const posName: string = position.Name;
let createExecutivePosition: CreateExecutivePositionCommand = {
position: posName,
}
await ExecutivePositionCommandHandler.create(createExecutivePosition);
}
// Delete statuses
let states = await MembershipStatusService.getAll(); let states = await MembershipStatusService.getAll();
for (const state of states) { for (const state of states) {
let deleteMembershipStatus: DeleteMembershipStatusCommand = { let deleteMembershipStatus: DeleteMembershipStatusCommand = {
@ -129,9 +161,56 @@ router.get("/", async (req, res) => {
await MembershipStatusCommandHandler.delete(deleteMembershipStatus); await MembershipStatusCommandHandler.delete(deleteMembershipStatus);
} }
let sStatesByName : any = {}; // Create Awards
const sAwardsById :any = {};
const sAwardsByName :any = {};
const awardNames = ['Ehrennadel Silber', 'Ehrennadel Gold', 'Ehrenkreuz Silber', 'Ehrenkreuz Gold', 'Ehrenmitgliedschaft', 'Ehrung 25 Jahre aktiv', 'Ehrung 40 Jahre aktiv'];
for (const awardName of awardNames) {
let createAward: CreateAwardCommand = {
award: awardName,
};
const awardId = await AwardCommandHandler.create(createAward);
console.log(`Created award ${awardName} with id ${awardId}`);
sAwardsById[awardId] = awardName;
sAwardsByName[awardName] = awardId;
}
// Create communication types
const createCommunicationTypeMail: CreateCommunicationTypeCommand = {
type: "Post", useColumns: ['city','street','streetNumber','streetNumberAddition']
}
const createCommunicationTypeEMail: CreateCommunicationTypeCommand = {
type: "Email", useColumns: ['email']
}
const createCommunicationTypeSMS: CreateCommunicationTypeCommand = {
type: "SMS", useColumns: ['mobile']
}
const createCommunicationTypeWhatsApp: CreateCommunicationTypeCommand = {
type: "WhatsApp", useColumns: ['mobile']
}
const createCommTypes = [createCommunicationTypeMail, createCommunicationTypeEMail, createCommunicationTypeSMS, createCommunicationTypeWhatsApp];
const sCommTypesByName: any = {};
const sCommTypesById: any = {};
for (const createCommType of createCommTypes) {
const commTypeId = await CommunicationTypeCommandHandler.create(createCommType);
console.log(`Create communication type ${createCommType.type} with id ${commTypeId}`);
sCommTypesByName[createCommType.type] = commTypeId;
sCommTypesById[commTypeId] = createCommType.type;
}
// Create positions
const sFunktionenByOldId :any = {};
let positionsResult: any[] = await db('Funktionen');
for (const position of positionsResult) {
const posName: string = position.Name.trim();
let createExecutivePosition: CreateExecutivePositionCommand = {
position: posName,
}
sFunktionenByOldId[position.IdFunktion] = await ExecutivePositionCommandHandler.create(createExecutivePosition);
}
// create membership states // create membership states
let sStatesByName : any = {};
for (const state of ['aktiv', 'passiv', 'fördernd']) { for (const state of ['aktiv', 'passiv', 'fördernd']) {
let createMembershipStatus: CreateMembershipStatusCommand = { let createMembershipStatus: CreateMembershipStatusCommand = {
status: state status: state
@ -141,8 +220,8 @@ router.get("/", async (req, res) => {
// Add all members // Add all members
let updateMembership;
for (const queryResultElement of queryResult) { for (const queryResultElement of queryResult) {
const ineternalID = queryResultElement.ID
let nameAffix: string = ""; let nameAffix: string = "";
let nn = queryResultElement.Nachname.trim(); let nn = queryResultElement.Nachname.trim();
if (nn.endsWith(", jun.")) { if (nn.endsWith(", jun.")) {
@ -154,7 +233,6 @@ router.get("/", async (req, res) => {
nn = nn.substring(0, nn.length - 6); nn = nn.substring(0, nn.length - 6);
} }
// before 1.7.2003: Altersobergrenze für Aktive: 60 // before 1.7.2003: Altersobergrenze für Aktive: 60
const limit60 = moment('2003-07-01'); const limit60 = moment('2003-07-01');
// 1.7.2003 bis 10.12.2011: 63 // 1.7.2003 bis 10.12.2011: 63
@ -174,13 +252,11 @@ router.get("/", async (req, res) => {
lastname: nn, lastname: nn,
nameaffix: nameAffix, nameaffix: nameAffix,
birthdate: queryResultElement.Geboren ? queryResultElement.Geboren : "1900-01-01", birthdate: queryResultElement.Geboren ? queryResultElement.Geboren : "1900-01-01",
internalId: queryResultElement.ID,
}; };
const memberId = await MemberCommandHandler.create(newMember); const memberId = await MemberCommandHandler.create(newMember);
console.log(`Created member ${newMember.firstname} ${newMember.lastname} with id ${memberId}`); console.log(`Created member ${newMember.firstname} ${newMember.lastname} with id ${memberId}`);
if (queryResultElement.ID === 567) {
console.log('jetzt');
}
let stateId = sStatesByName["aktiv"]; let stateId = sStatesByName["aktiv"];
if (!queryResultElement.Eingetreten) { if (!queryResultElement.Eingetreten) {
throw new Error('Eingetreten missing'); throw new Error('Eingetreten missing');
@ -190,7 +266,7 @@ router.get("/", async (req, res) => {
} }
let start = queryResultElement.Eingetreten; let start = queryResultElement.Eingetreten;
let createMembership: CreateMembershipCommand = { let createMembership: CreateMembershipCommand = {
internalId: queryResultElement.ID, memberId: memberId, start: start, statusId: stateId, memberId: memberId, start: start, statusId: stateId,
}; };
let ms1 = await MembershipCommandHandler.create(createMembership); let ms1 = await MembershipCommandHandler.create(createMembership);
@ -229,7 +305,6 @@ router.get("/", async (req, res) => {
let updateMembership: UpdateMembershipCommand = { let updateMembership: UpdateMembershipCommand = {
end: end, end: end,
id: ms1, id: ms1,
internalId: queryResultElement.ID,
memberId: memberId, memberId: memberId,
start: start, start: start,
statusId: stateId, statusId: stateId,
@ -297,7 +372,6 @@ router.get("/", async (req, res) => {
let updateMembership: UpdateMembershipCommand = { let updateMembership: UpdateMembershipCommand = {
end: end, end: end,
id: ms1, id: ms1,
internalId: queryResultElement.ID,
memberId: memberId, memberId: memberId,
start: start, start: start,
statusId: stateId, statusId: stateId,
@ -310,7 +384,6 @@ router.get("/", async (req, res) => {
stateId = newStateId; stateId = newStateId;
start = newStart; start = newStart;
let createMembership: CreateMembershipCommand = { let createMembership: CreateMembershipCommand = {
// internalId: queryResultElement.ID,
memberId: memberId, memberId: memberId,
start: start, start: start,
statusId: stateId, statusId: stateId,
@ -325,7 +398,6 @@ router.get("/", async (req, res) => {
let updateMembership: UpdateMembershipCommand = { let updateMembership: UpdateMembershipCommand = {
end: queryResultElement.Ausgetreten, end: queryResultElement.Ausgetreten,
id: ms1, id: ms1,
// internalId: queryResultElement.ID,
memberId: memberId, memberId: memberId,
start: start, start: start,
statusId: stateId, statusId: stateId,
@ -338,7 +410,6 @@ router.get("/", async (req, res) => {
let updateMembership: UpdateMembershipCommand = { let updateMembership: UpdateMembershipCommand = {
end: queryResultElement.verstorben, end: queryResultElement.verstorben,
id: ms1, id: ms1,
// internalId: queryResultElement.ID,
memberId: memberId, memberId: memberId,
start: start, start: start,
statusId: stateId, statusId: stateId,
@ -348,9 +419,9 @@ router.get("/", async (req, res) => {
} }
// add member's awards
for (const awardId in sAwardsById) { for (const awardId in sAwardsById) {
const awardName = sAwardsById[awardId]; const awardName = sAwardsById[awardId];
if (queryResultElement[awardName]) { if (queryResultElement[awardName]) {
const awardDate = queryResultElement[awardName]; const awardDate = queryResultElement[awardName];
console.log(`Member ${newMember.firstname} ${newMember.lastname} got award ${awardName} at ${awardDate}`); console.log(`Member ${newMember.firstname} ${newMember.lastname} got award ${awardName} at ${awardDate}`);
@ -375,9 +446,103 @@ router.get("/", async (req, res) => {
}; };
await MemberAwardCommandHandler.create(newMemberAward); await MemberAwardCommandHandler.create(newMemberAward);
} }
} }
// add member's executive positions (if there are any)
let memberPositionsResult: any[] = await db('MitgliedFunktionen').where('MitgliedFunktionen.IdMitglied', '=', ineternalID);
for (const position of memberPositionsResult) {
const start = position.Beginn;
const end = position.Ende;
const executivePositionId: number = sFunktionenByOldId[position.IdFunktion]
let createMemberExecutivePosition: CreateMemberExecutivePositionCommand = {
end: end, executivePositionId: executivePositionId, memberId: memberId, note: "", start: start
}
console.log(`Adding position ${executivePositionId} to ${newMember.firstname} ${newMember.lastname}`);
await MemberExecutivePositionCommandHandler.create(createMemberExecutivePosition);
}
// add communications
const createCommunicationCommands:CreateCommunicationCommand[] = [];
let havePreferred = false;
if (queryResultElement.Mobiltelefon && !queryResultElement.verstorben && !queryResultElement.verzogenDatum) {
const createCommunicationMobile: CreateCommunicationCommand = {
memberId: memberId,
typeId: sCommTypesByName['SMS'],
isSMSAlarming: false,
city: "",
email: "",
mobile: queryResultElement.Mobiltelefon.trim(),
preferred: false,
street: "",
streetNumber: 0,
streetNumberAddition: "",
}
createCommunicationCommands.push(createCommunicationMobile);
}
if (queryResultElement.email && !queryResultElement.verstorben && !queryResultElement.verzogenDatum) {
const createCommunicationEmail: CreateCommunicationCommand = {
memberId: memberId,
typeId: sCommTypesByName['Email'],
isSMSAlarming: false,
city: "",
email: queryResultElement.email,
mobile: "",
preferred: queryResultElement.EinladungNurPerEmail,
street: "",
streetNumber: 0,
streetNumberAddition: "",
}
if (createCommunicationEmail.preferred) {
havePreferred = true;
}
createCommunicationCommands.push(createCommunicationEmail);
}
if (queryResultElement.Mobiltelefon && queryResultElement.EinladungNurPerEmail && !queryResultElement.email && !queryResultElement.verstorben && !queryResultElement.verzogenDatum) {
const preferred = havePreferred === false;
const createCommunicationWhatsApp: CreateCommunicationCommand = {
memberId: memberId,
typeId: sCommTypesByName['WhatsApp'],
isSMSAlarming: false,
city: "",
email: "",
mobile: queryResultElement.Mobiltelefon.trim(),
preferred: preferred,
street: "",
streetNumber: 0,
streetNumberAddition: "",
}
if (createCommunicationWhatsApp.preferred) {
havePreferred = true;
}
createCommunicationCommands.push(createCommunicationWhatsApp);
}
if (queryResultElement.Ort && queryResultElement.street) {
const preferred = havePreferred === false;
const createCommunicationMail: CreateCommunicationCommand = {
memberId: memberId,
typeId: sCommTypesByName['Post'],
isSMSAlarming: false,
city: queryResultElement.Ort,
email: "",
mobile: "",
preferred: preferred,
street: queryResultElement.street.trim(),
streetNumber: queryResultElement.streetnumber,
streetNumberAddition: queryResultElement.streetnumberaddition.trim(),
}
if (createCommunicationMail.preferred) {
havePreferred = true;
}
createCommunicationCommands.push(createCommunicationMail);
}
if (createCommunicationCommands.length > 0 && !havePreferred) {
console.log(`No preferred communication for ${newMember.firstname} ${newMember.lastname}`)
}
for (const createCommunicationCommand of createCommunicationCommands) {
console.log(`Adding communication for ${sCommTypesById[createCommunicationCommand.typeId]} to ${newMember.firstname} ${newMember.lastname}`)
await CommunicationCommandHandler.create(createCommunicationCommand);
}
} }