diff --git a/src/routes/fillfromold.ts b/src/routes/fillfromold.ts index 37fbd32..7ab59ff 100644 --- a/src/routes/fillfromold.ts +++ b/src/routes/fillfromold.ts @@ -9,6 +9,9 @@ import { CreateMemberCommand, DeleteMemberCommand } from "../command/memberComma 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"; var router = express.Router({ mergeParams: true }); @@ -43,10 +46,20 @@ router.get("/", async (req, res) => { let queryResult = await db('Mitglieder'); console.log(`Read ${queryResult.length} Mitglieder`); - // Delete members let [members, total] = await MemberService.getAll(0, 9999); for (const m of members) { + + // delete all member awards of this member + let memberAwards = await MemberAwardService.getAll(m.id); + for (const award of memberAwards) { + let deleteMemberAward: DeleteMemberAwardCommand = { + id: award.id, memberId: m.id + }; + console.log(`Deleting award ${award.id} from member ${m.id}`); + await MemberAwardCommandHandler.delete(deleteMemberAward) + } + let deleteMember: DeleteMemberCommand = { id: m.id, }; @@ -65,9 +78,10 @@ router.get("/", async (req, res) => { } const sAwardsById :any = {}; + const sAwardsByName :any = {}; // Create Awards - const awardNames = ['Ehrennadel Silber', 'Ehrennadel Gold', 'Ehrenkreuz Silber', 'Ehrenkreuz Gold', 'Ehrenmitgliedschaft', '25 Jahre aktiv', '40 Jahre aktiv']; + 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, @@ -75,6 +89,7 @@ router.get("/", async (req, res) => { const awardId = await AwardCommandHandler.create(createAward); console.log(`Created award ${awardName} with id ${awardId}`); sAwardsById[awardId] = awardName; + sAwardsByName[awardName] = awardId; } for (const queryResultElement of queryResult) { @@ -97,6 +112,38 @@ router.get("/", async (req, res) => { } const memberId = await MemberCommandHandler.create(newMember); console.log(`Created member ${newMember.firstname} ${newMember.lastname} with id ${memberId}`); + + for (const awardId in sAwardsById) { + const awardName = sAwardsById[awardId]; + + if (queryResultElement[awardName]) { + const awardDate = queryResultElement[awardName]; + 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) { + given = false; + note = queryResultElement['keineEhrung25JahreAktivGrund'].trim(); + } + if (awardName === 'Ehrennadel Silber' && queryResultElement['keineEhrennadelSilberGrund'] !== null) { + given = false; + note = queryResultElement['keineEhrennadelSilberGrund'].trim(); + } + if (awardName === 'Ehrennadel Gold' && queryResultElement['keineEhrennadelGoldGrund'] !== null) { + given = false; + note = queryResultElement['keineEhrennadelGoldGrund'].trim(); + } + + let newMemberAward: CreateMemberAwardCommand = { + awardId: parseInt(awardId), date: awardDate, given: given, memberId: memberId, note: note + } + await MemberAwardCommandHandler.create(newMemberAward); + } + + } + + } res.sendStatus(200);