diff --git a/src/routes/fillfromold.ts b/src/routes/fillfromold.ts index 7ab59ff..7beba27 100644 --- a/src/routes/fillfromold.ts +++ b/src/routes/fillfromold.ts @@ -1,17 +1,24 @@ import express from "express"; -import knex from 'knex'; +import knex from "knex"; +import moment from "moment"; import AwardService from "../service/awardService"; import { CreateAwardCommand, DeleteAwardCommand } from "../command/awardCommand"; import AwardCommandHandler from "../command/awardCommandHandler"; -import { award } from "../entity/award"; import MemberService from "../service/memberService"; import { CreateMemberCommand, DeleteMemberCommand } from "../command/memberCommand"; -import { member } from "../entity/member"; import MemberCommandHandler from "../command/memberCommandHandler"; -import { Salutation } from "../enums/salutation"; import MemberAwardCommandHandler from "../command/memberAwardCommandHandler"; import { CreateMemberAwardCommand, DeleteMemberAwardCommand } from "../command/memberAwardCommand"; import MemberAwardService from "../service/memberAwardService"; +import { CreateMembershipStatusCommand, DeleteMembershipStatusCommand } from "../command/membershipStatusCommand"; +import MembershipStatusCommandHandler from "../command/membershipStatusCommandHandler"; +import { CreateExecutivePositionCommand, DeleteExecutivePositionCommand } from "../command/executivePositionCommand"; +import ExecutivePositionCommandHandler from "../command/executivePositionCommandHandler"; +import ExecutivePositionService from "../service/executivePositionService"; +import MembershipStatusService from "../service/membershipStatusService"; +import { CreateMembershipCommand, UpdateMembershipCommand } from "../command/membershipCommand"; +import MembershipCommandHandler from "../command/membershipCommandHandler"; +import { updateMembershipOfMember } from "../controller/admin/memberController"; var router = express.Router({ mergeParams: true }); @@ -92,15 +99,58 @@ router.get("/", async (req, res) => { sAwardsByName[awardName] = awardId; } + // Delete postions + let positions = await ExecutivePositionService.getAll(); + for (const position of positions) { + let deletePosition: DeleteExecutivePositionCommand = { + id: position.id, + }; + console.log(`Delete position ${position.position}`); + await ExecutivePositionCommandHandler.delete(deletePosition); + } + + // Create positions + 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(); + for (const state of states) { + let deleteMembershipStatus: DeleteMembershipStatusCommand = { + id: state.id + }; + console.log(`Delete membership status ${state.status}`); + await MembershipStatusCommandHandler.delete(deleteMembershipStatus); + } + + let sStatesByName : any = {}; + + // create membership states + for (const state of ['aktiv', 'passiv', 'fördernd']) { + let createMembershipStatus: CreateMembershipStatusCommand = { + status: state + } + sStatesByName[state] = await MembershipStatusCommandHandler.create(createMembershipStatus); + } + + + // Add all members + let updateMembership; for (const queryResultElement of queryResult) { - let nameAffix :string = ''; + let nameAffix: string = ""; let nn = queryResultElement.Nachname.trim(); - if (nn.endsWith(', jun.')) { - nameAffix = 'jun.'; + if (nn.endsWith(", jun.")) { + nameAffix = "jun."; nn = nn.substring(0, nn.length - 6); } - if (nn.endsWith(', sen.')) { - nameAffix = 'sen.'; + if (nn.endsWith(", sen.")) { + nameAffix = "sen."; nn = nn.substring(0, nn.length - 6); } let newMember: CreateMemberCommand = { @@ -108,11 +158,110 @@ router.get("/", async (req, res) => { firstname: queryResultElement.Vorname.trim(), lastname: nn, nameaffix: nameAffix, - birthdate: queryResultElement.Geboren ? queryResultElement.Geboren : '1900-01-01', - } + birthdate: queryResultElement.Geboren ? queryResultElement.Geboren : "1900-01-01", + }; const memberId = await MemberCommandHandler.create(newMember); console.log(`Created member ${newMember.firstname} ${newMember.lastname} with id ${memberId}`); + let stateId = sStatesByName["aktiv"]; + if (queryResultElement["Übergang Passiv"] === queryResultElement.Eingetreten) { + stateId = sStatesByName["fördernd"]; + } + let start = queryResultElement.Eingetreten; + let createMembership: CreateMembershipCommand = { + internalId: queryResultElement.ID, memberId: memberId, start: start, statusId: stateId, + }; + let ms1 = await MembershipCommandHandler.create(createMembership); + + if (queryResultElement["Übergang Passiv"] && queryResultElement["Übergang Passiv"] !== queryResultElement.Eingetreten) { + // membership transitioned to passiv + // no knowledge about active time => assume active until age of 60 or 65 + let end: Date = queryResultElement["Übergang Passiv"]; + let terminationReason : string = "aktiv >> passiv"; + let newStart = queryResultElement["Übergang Passiv"]; + let newStateId = sStatesByName["passiv"]; + + if (queryResultElement.Geboren == null && queryResultElement.verzogenDatum) { + terminationReason = "verzogen"; + end = queryResultElement.verzogenDatum + } else { + if (queryResultElement["Übergang Passiv"] < queryResultElement.Eingetreten) { + // before 1.7.2003: Altersobergrenze für Aktive: 60 + const limit60 = moment('2003-07-01'); + // 1.7.2003 bis 10.12.2011: 63 + const limit63 = moment('2011-12-10'); + // from 10.12.2011: 65 + + const alter60 = moment(queryResultElement.Geboren).add(60, 'years'); + const alter63 = moment(queryResultElement.Geboren).add(63, 'years'); + const alter65 = moment(queryResultElement.Geboren).add(65, 'years'); + + if (alter60.isBefore(limit60)) { + terminationReason = "passiv (Altersobergrenze: 60)"; + end = alter60.toDate(); + } else { + if (alter63.isBefore(limit63)) { + terminationReason = "passiv (Altersobergrenze: 63)"; + end = alter63.toDate(); + } else { + terminationReason = "passiv (Altersobergrenze: 65)"; + end = alter65.toDate(); + } + } + } + } + newStart = end; + // finish current membership + let updateMembership: UpdateMembershipCommand = { + end: end, + id: ms1, + internalId: queryResultElement.ID, + memberId: memberId, + start: start, + statusId: stateId, + terminationReason: terminationReason, + }; + await MembershipCommandHandler.update(updateMembership); + + // create new membership with passiv state + stateId = newStateId; + start = newStart; + let createMembership: CreateMembershipCommand = { + // internalId: queryResultElement.ID, + memberId: memberId, + start: start, + statusId: stateId, + }; + ms1 = await MembershipCommandHandler.create(createMembership); + } + + if (queryResultElement.Ausgetreten) { + let updateMembership: UpdateMembershipCommand = { + end: queryResultElement.Ausgetreten, + id: ms1, + // internalId: queryResultElement.ID, + memberId: memberId, + start: start, + statusId: stateId, + terminationReason: "Austritt", + }; + await MembershipCommandHandler.update(updateMembership); + } + + if (queryResultElement.verstorben) { + let updateMembership: UpdateMembershipCommand = { + end: queryResultElement.verstorben, + id: ms1, + // internalId: queryResultElement.ID, + memberId: memberId, + start: start, + statusId: stateId, + terminationReason: "verstorben", + }; + await MembershipCommandHandler.update(updateMembership); + } + + for (const awardId in sAwardsById) { const awardName = sAwardsById[awardId]; @@ -121,23 +270,23 @@ router.get("/", async (req, res) => { console.log(`Member ${newMember.firstname} ${newMember.lastname} got award ${awardName} at ${awardDate}`); let given = true; - let note = ''; - if (awardName === 'Ehrung 25 Jahre aktiv' && queryResultElement['keineEhrung25JahreAktivGrund'] !== null) { + let note = ""; + if (awardName === "Ehrung 25 Jahre aktiv" && queryResultElement["keineEhrung25JahreAktivGrund"] !== null) { given = false; - note = queryResultElement['keineEhrung25JahreAktivGrund'].trim(); + note = queryResultElement["keineEhrung25JahreAktivGrund"].trim(); } - if (awardName === 'Ehrennadel Silber' && queryResultElement['keineEhrennadelSilberGrund'] !== null) { + if (awardName === "Ehrennadel Silber" && queryResultElement["keineEhrennadelSilberGrund"] !== null) { given = false; - note = queryResultElement['keineEhrennadelSilberGrund'].trim(); + note = queryResultElement["keineEhrennadelSilberGrund"].trim(); } - if (awardName === 'Ehrennadel Gold' && queryResultElement['keineEhrennadelGoldGrund'] !== null) { + if (awardName === "Ehrennadel Gold" && queryResultElement["keineEhrennadelGoldGrund"] !== null) { given = false; - note = queryResultElement['keineEhrennadelGoldGrund'].trim(); + note = queryResultElement["keineEhrennadelGoldGrund"].trim(); } let newMemberAward: CreateMemberAwardCommand = { - awardId: parseInt(awardId), date: awardDate, given: given, memberId: memberId, note: note - } + awardId: parseInt(awardId), date: awardDate, given: given, memberId: memberId, note: note, + }; await MemberAwardCommandHandler.create(newMemberAward); }