From a91b723f045e674873a84eb03a97b9f5c27aa93d Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Thu, 30 Jan 2025 14:02:42 +0100 Subject: [PATCH] default arrays and none existant data inserts --- src/helpers/backupHelper.ts | 82 ++++++++++++++++++++++++++++++------- 1 file changed, 68 insertions(+), 14 deletions(-) diff --git a/src/helpers/backupHelper.ts b/src/helpers/backupHelper.ts index 8deff02..6d18f7a 100644 --- a/src/helpers/backupHelper.ts +++ b/src/helpers/backupHelper.ts @@ -20,7 +20,10 @@ export type BackupSectionRefered = { [key in BackupSection]?: Array; }; -export type BackupFileContent = { [key in BackupSection]?: BackupFileContentSection } & { collectIds: boolean }; +export type BackupFileContent = { [key in BackupSection]?: BackupFileContentSection } & { + collectIds: boolean; + version: 1; +}; export type BackupFileContentSection = Array | { [key: string]: Array }; export default abstract class BackupHelper { @@ -87,7 +90,7 @@ export default abstract class BackupHelper { filename = new Date().toISOString().split("T")[0]; } - let json: BackupFileContent = { collectIds }; + let json: BackupFileContent = { collectIds, version: 1 }; for (const section of this.backupSection) { json[section.type] = await this.getSectionData(section.type, collectIds); } @@ -424,6 +427,30 @@ export default abstract class BackupHelper { } private static async setMemberData(data: Array): Promise { + await this.setMemberBase({ + award: data + .map((d) => d.awards.map((c: any) => c.award)) + .flat() + .map((d) => ({ ...d, id: undefined })), + communication_type: data + .map((d) => d.communications.map((c: any) => c.type)) + .flat() + .map((d) => ({ ...d, id: undefined })), + executive_position: data + .map((d) => d.positions.map((c: any) => c.executivePosition)) + .flat() + .map((d) => ({ ...d, id: undefined })), + membership_status: data + .map((d) => d.memberships.map((c: any) => c.status)) + .flat() + .map((d) => ({ ...d, id: undefined })), + salutation: data.map((d) => d.salutation).map((d) => ({ ...d, id: undefined })), + qualification: data + .map((d) => d.qualifications.map((c: any) => c.qualification)) + .flat() + .map((d) => ({ ...d, id: undefined })), + }); + let salutation = await this.transactionManager.getRepository("salutation").find(); let communication = await this.transactionManager.getRepository("communication_type").find(); let membership = await this.transactionManager.getRepository("membership_status").find(); @@ -479,42 +506,42 @@ export default abstract class BackupHelper { .createQueryBuilder() .insert() .into("award") - .values(data["award"]) + .values(data?.["award"] ?? []) .orIgnore() .execute(); await this.transactionManager .createQueryBuilder() .insert() .into("communication_type") - .values(data["communication_type"]) + .values(data?.["communication_type"] ?? []) .orIgnore() .execute(); await this.transactionManager .createQueryBuilder() .insert() .into("executive_position") - .values(data["executive_position"]) + .values(data?.["executive_position"] ?? []) .orIgnore() .execute(); await this.transactionManager .createQueryBuilder() .insert() .into("membership_status") - .values(data["membership_status"]) + .values(data?.["membership_status"] ?? []) .orIgnore() .execute(); await this.transactionManager .createQueryBuilder() .insert() .into("salutation") - .values(data["salutation"]) + .values(data?.["salutation"] ?? []) .orIgnore() .execute(); await this.transactionManager .createQueryBuilder() .insert() .into("qualification") - .values(data["qualification"]) + .values(data?.["qualification"] ?? []) .orIgnore() .execute(); } @@ -543,6 +570,8 @@ export default abstract class BackupHelper { await this.transactionManager.getRepository("protocol").save(dataWithMappedIds); } private static async setNewsletter(data: Array, collectedIds: boolean): Promise { + await this.setQueryStore(data.map((d) => d.recipientsByQueryId).map((d) => ({ ...d, id: undefined }))); + let queries = await this.transactionManager.getRepository("query").find(); let members = await this.transactionManager.getRepository("member").find(); let dataWithMappedIds = data.map((d) => ({ @@ -572,6 +601,10 @@ export default abstract class BackupHelper { await this.transactionManager.getRepository("newsletter").save(dataWithMappedIds); } private static async setNewsletterConfig(data: Array): Promise { + await this.setMemberBase({ + communication_type: data.map((d) => d.comType).map((d) => ({ ...d, id: undefined })), + }); + let types = await this.transactionManager.getRepository("communication_type").find(); let dataWithMappedIds = data.map((d) => ({ ...d, @@ -583,9 +616,18 @@ export default abstract class BackupHelper { await this.transactionManager.getRepository("newsletter_config").save(dataWithMappedIds); } private static async setCalendar(data: { [key: string]: Array }): Promise { - await this.transactionManager.getRepository("calendar_type").save(data["calendar_type"]); + let usedTypes = (data?.["calendar"] ?? []).map((d) => d.type).map((d) => ({ ...d, id: undefined })); + + await this.transactionManager + .createQueryBuilder() + .insert() + .into("award") + .values([...(data?.["calendar_type"] ?? []), ...usedTypes]) + .orIgnore() + .execute(); + let types = await this.transactionManager.getRepository("calendar_type").find(); - let dataWithMappedIds = data["calendar"].map((c) => ({ + let dataWithMappedIds = (data?.["calendar"] ?? []).map((c) => ({ ...c, type: { ...c.type, @@ -602,21 +644,33 @@ export default abstract class BackupHelper { .createQueryBuilder() .insert() .into("template") - .values(data["template"]) + .values(data?.["template"] ?? []) .orIgnore() .execute(); await this.transactionManager .createQueryBuilder() .insert() .into("template_usage") - .values(data["template_usage"]) + .values(data?.["template_usage"] ?? []) .orIgnore() .execute(); } private static async setUser(data: { [key: string]: Array }): Promise { - await this.transactionManager.createQueryBuilder().insert().into("role").values(data["role"]).orIgnore().execute(); + let usedRoles = (data?.["user"] ?? []) + .map((d) => d.roles) + .flat() + .map((d) => ({ ...d, id: undefined })); + + await this.transactionManager + .createQueryBuilder() + .insert() + .into("role") + .values([...(data?.["role"] ?? []), ...usedRoles]) + .orIgnore() + .execute(); + let roles = await this.transactionManager.getRepository("role").find(); - let dataWithMappedIds = data["user"].map((u) => ({ + let dataWithMappedIds = (data?.["user"] ?? []).map((u) => ({ ...u, roles: u.roles.map((r: any) => ({ ...r,