default arrays and none existant data inserts

This commit is contained in:
Julian Krauser 2025-01-30 14:02:42 +01:00
parent 5701313228
commit a91b723f04

View file

@ -20,7 +20,10 @@ export type BackupSectionRefered = {
[key in BackupSection]?: Array<string>; [key in BackupSection]?: Array<string>;
}; };
export type BackupFileContent = { [key in BackupSection]?: BackupFileContentSection } & { collectIds: boolean }; export type BackupFileContent = { [key in BackupSection]?: BackupFileContentSection } & {
collectIds: boolean;
version: 1;
};
export type BackupFileContentSection = Array<any> | { [key: string]: Array<any> }; export type BackupFileContentSection = Array<any> | { [key: string]: Array<any> };
export default abstract class BackupHelper { export default abstract class BackupHelper {
@ -87,7 +90,7 @@ export default abstract class BackupHelper {
filename = new Date().toISOString().split("T")[0]; filename = new Date().toISOString().split("T")[0];
} }
let json: BackupFileContent = { collectIds }; let json: BackupFileContent = { collectIds, version: 1 };
for (const section of this.backupSection) { for (const section of this.backupSection) {
json[section.type] = await this.getSectionData(section.type, collectIds); json[section.type] = await this.getSectionData(section.type, collectIds);
} }
@ -424,6 +427,30 @@ export default abstract class BackupHelper {
} }
private static async setMemberData(data: Array<any>): Promise<void> { private static async setMemberData(data: Array<any>): Promise<void> {
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 salutation = await this.transactionManager.getRepository("salutation").find();
let communication = await this.transactionManager.getRepository("communication_type").find(); let communication = await this.transactionManager.getRepository("communication_type").find();
let membership = await this.transactionManager.getRepository("membership_status").find(); let membership = await this.transactionManager.getRepository("membership_status").find();
@ -479,42 +506,42 @@ export default abstract class BackupHelper {
.createQueryBuilder() .createQueryBuilder()
.insert() .insert()
.into("award") .into("award")
.values(data["award"]) .values(data?.["award"] ?? [])
.orIgnore() .orIgnore()
.execute(); .execute();
await this.transactionManager await this.transactionManager
.createQueryBuilder() .createQueryBuilder()
.insert() .insert()
.into("communication_type") .into("communication_type")
.values(data["communication_type"]) .values(data?.["communication_type"] ?? [])
.orIgnore() .orIgnore()
.execute(); .execute();
await this.transactionManager await this.transactionManager
.createQueryBuilder() .createQueryBuilder()
.insert() .insert()
.into("executive_position") .into("executive_position")
.values(data["executive_position"]) .values(data?.["executive_position"] ?? [])
.orIgnore() .orIgnore()
.execute(); .execute();
await this.transactionManager await this.transactionManager
.createQueryBuilder() .createQueryBuilder()
.insert() .insert()
.into("membership_status") .into("membership_status")
.values(data["membership_status"]) .values(data?.["membership_status"] ?? [])
.orIgnore() .orIgnore()
.execute(); .execute();
await this.transactionManager await this.transactionManager
.createQueryBuilder() .createQueryBuilder()
.insert() .insert()
.into("salutation") .into("salutation")
.values(data["salutation"]) .values(data?.["salutation"] ?? [])
.orIgnore() .orIgnore()
.execute(); .execute();
await this.transactionManager await this.transactionManager
.createQueryBuilder() .createQueryBuilder()
.insert() .insert()
.into("qualification") .into("qualification")
.values(data["qualification"]) .values(data?.["qualification"] ?? [])
.orIgnore() .orIgnore()
.execute(); .execute();
} }
@ -543,6 +570,8 @@ export default abstract class BackupHelper {
await this.transactionManager.getRepository("protocol").save(dataWithMappedIds); await this.transactionManager.getRepository("protocol").save(dataWithMappedIds);
} }
private static async setNewsletter(data: Array<any>, collectedIds: boolean): Promise<void> { private static async setNewsletter(data: Array<any>, collectedIds: boolean): Promise<void> {
await this.setQueryStore(data.map((d) => d.recipientsByQueryId).map((d) => ({ ...d, id: undefined })));
let queries = await this.transactionManager.getRepository("query").find(); let queries = await this.transactionManager.getRepository("query").find();
let members = await this.transactionManager.getRepository("member").find(); let members = await this.transactionManager.getRepository("member").find();
let dataWithMappedIds = data.map((d) => ({ let dataWithMappedIds = data.map((d) => ({
@ -572,6 +601,10 @@ export default abstract class BackupHelper {
await this.transactionManager.getRepository("newsletter").save(dataWithMappedIds); await this.transactionManager.getRepository("newsletter").save(dataWithMappedIds);
} }
private static async setNewsletterConfig(data: Array<any>): Promise<void> { private static async setNewsletterConfig(data: Array<any>): Promise<void> {
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 types = await this.transactionManager.getRepository("communication_type").find();
let dataWithMappedIds = data.map((d) => ({ let dataWithMappedIds = data.map((d) => ({
...d, ...d,
@ -583,9 +616,18 @@ export default abstract class BackupHelper {
await this.transactionManager.getRepository("newsletter_config").save(dataWithMappedIds); await this.transactionManager.getRepository("newsletter_config").save(dataWithMappedIds);
} }
private static async setCalendar(data: { [key: string]: Array<any> }): Promise<void> { private static async setCalendar(data: { [key: string]: Array<any> }): Promise<void> {
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 types = await this.transactionManager.getRepository("calendar_type").find();
let dataWithMappedIds = data["calendar"].map((c) => ({ let dataWithMappedIds = (data?.["calendar"] ?? []).map((c) => ({
...c, ...c,
type: { type: {
...c.type, ...c.type,
@ -602,21 +644,33 @@ export default abstract class BackupHelper {
.createQueryBuilder() .createQueryBuilder()
.insert() .insert()
.into("template") .into("template")
.values(data["template"]) .values(data?.["template"] ?? [])
.orIgnore() .orIgnore()
.execute(); .execute();
await this.transactionManager await this.transactionManager
.createQueryBuilder() .createQueryBuilder()
.insert() .insert()
.into("template_usage") .into("template_usage")
.values(data["template_usage"]) .values(data?.["template_usage"] ?? [])
.orIgnore() .orIgnore()
.execute(); .execute();
} }
private static async setUser(data: { [key: string]: Array<any> }): Promise<void> { private static async setUser(data: { [key: string]: Array<any> }): Promise<void> {
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 roles = await this.transactionManager.getRepository("role").find();
let dataWithMappedIds = data["user"].map((u) => ({ let dataWithMappedIds = (data?.["user"] ?? []).map((u) => ({
...u, ...u,
roles: u.roles.map((r: any) => ({ roles: u.roles.map((r: any) => ({
...r, ...r,