From 3ea7c423c9b291d06f20c5dd30bcd570505292b2 Mon Sep 17 00:00:00 2001 From: Anton Schegg Date: Sun, 29 Dec 2024 21:50:22 +0100 Subject: [PATCH] import from old database --- package.json | 2 + src/routes/fillfromold.ts | 105 ++++++++++++++++++++++++++++++++++++++ src/routes/index.ts | 2 + 3 files changed, 109 insertions(+) create mode 100644 src/routes/fillfromold.ts diff --git a/package.json b/package.json index 2020d05..c64cfe7 100644 --- a/package.json +++ b/package.json @@ -30,8 +30,10 @@ "handlebars": "^4.7.8", "ics": "^3.8.1", "jsonwebtoken": "^9.0.2", + "knex": "^3.1.0", "moment": "^2.30.1", "ms": "^2.1.3", + "mssql": "^10.0.4", "mysql": "^2.18.1", "node-schedule": "^2.1.1", "nodemailer": "^6.9.14", diff --git a/src/routes/fillfromold.ts b/src/routes/fillfromold.ts new file mode 100644 index 0000000..37fbd32 --- /dev/null +++ b/src/routes/fillfromold.ts @@ -0,0 +1,105 @@ +import express from "express"; +import knex from 'knex'; +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"; + +var router = express.Router({ mergeParams: true }); + +router.get("/", async (req, res) => { + + const dbhost = process.env.OLD_DB_HOST; + const dbport = Number(process.env.OLD_DB_PORT); + const dbuser = process.env.OLD_DB_USER; + const dbuserpass = process.env.OLD_DB_USERPASS; + const dbname = process.env.OLD_DB_NAME; + + const db = knex({ + client: 'mssql', + connection: { + host: dbhost, + port: dbport, + user: dbuser, + password: dbuserpass, + database: dbname, + options: { + trustServerCertificate: true, + enableArithAbort: true, + encrypt: true + } + }, + debug: false, + pool: { + min: 0, max: 30 + } + }); + + 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) { + let deleteMember: DeleteMemberCommand = { + id: m.id, + }; + console.log(`Delete member ${m.id}`); + await MemberCommandHandler.delete(deleteMember); + } + + // Delete awards + let awards = await AwardService.getAll(); + for (const award of awards) { + let deleteAward: DeleteAwardCommand = { + id: award.id, + }; + console.log(`Delete award ${award.id}`); + await AwardCommandHandler.delete(deleteAward); + } + + const sAwardsById :any = {}; + + // Create Awards + const awardNames = ['Ehrennadel Silber', 'Ehrennadel Gold', 'Ehrenkreuz Silber', 'Ehrenkreuz Gold', 'Ehrenmitgliedschaft', '25 Jahre aktiv', '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; + } + + for (const queryResultElement of queryResult) { + let nameAffix :string = ''; + let nn = queryResultElement.Nachname.trim(); + if (nn.endsWith(', jun.')) { + nameAffix = 'jun.'; + nn = nn.substring(0, nn.length - 6); + } + if (nn.endsWith(', sen.')) { + nameAffix = 'sen.'; + nn = nn.substring(0, nn.length - 6); + } + let newMember: CreateMemberCommand = { + salutation: queryResultElement.Anrede.trim(), + firstname: queryResultElement.Vorname.trim(), + lastname: nn, + nameaffix: nameAffix, + 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}`); + } + + res.sendStatus(200); +}); + +export default router; diff --git a/src/routes/index.ts b/src/routes/index.ts index 72cb526..5df873b 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -13,6 +13,7 @@ import reset from "./reset"; import auth from "./auth"; import admin from "./admin/index"; import user from "./user"; +import fillfromold from "./fillfromold"; export default (app: Express) => { app.set("query parser", "extended"); @@ -27,6 +28,7 @@ export default (app: Express) => { app.use("/api/public", publicAvailable); app.use("/api/setup", allowSetup, setup); + app.use("/api/fillfromold", fillfromold); app.use("/api/reset", reset); app.use("/api/invite", invite); app.use("/api/auth", auth);