newsletter syncing store

This commit is contained in:
Julian Krauser 2024-12-26 12:34:36 +01:00
parent 844bd9a8d5
commit 9ef171b913
41 changed files with 347 additions and 62 deletions

View file

@ -6,7 +6,7 @@ import type {
} from "@/viewmodels/admin/communicationType.models";
import { http } from "@/serverCom";
import type { AxiosResponse } from "axios";
import type { CommunicationFieldType } from "../../types/fieldTypes";
import type { CommunicationFieldType } from "@/types/fieldTypes";
export const useCommunicationTypeStore = defineStore("communicationType", {
state: () => {

View file

@ -0,0 +1,105 @@
import { defineStore } from "pinia";
import type { CreateNewsletterViewModel, SyncNewsletterViewModel } from "@/viewmodels/admin/newsletter.models";
import { http } from "@/serverCom";
import type { AxiosResponse } from "axios";
import type { NewsletterViewModel } from "@/viewmodels/admin/newsletter.models";
import cloneDeep from "lodash.clonedeep";
import isEqual from "lodash.isequal";
import difference from "lodash.difference";
export const useNewsletterStore = defineStore("newsletter", {
state: () => {
return {
newsletters: [] as Array<NewsletterViewModel & { tab_pos: number }>,
totalCount: 0 as number,
loading: "loading" as "loading" | "fetched" | "failed",
activeNewsletter: null as number | null,
activeNewsletterObj: null as NewsletterViewModel | null,
origin: null as NewsletterViewModel | null,
loadingActive: "loading" as "loading" | "fetched" | "failed",
syncingNewsletter: "synced" as "synced" | "syncing" | "detectedChanges" | "failed",
};
},
getters: {
detectedChangeNewsletter: (state) =>
!isEqual(state.origin, state.activeNewsletterObj) && state.syncingNewsletter != "syncing",
},
actions: {
setNewsletterSyncingState(state: "synced" | "syncing" | "detectedChanges" | "failed") {
this.syncingNewsletter = state;
},
fetchNewsletters(offset = 0, count = 25, clear = false) {
if (clear) this.newsletters = [];
this.loading = "loading";
http
.get(`/admin/newsletter?offset=${offset}&count=${count}`)
.then((result) => {
this.totalCount = result.data.total;
result.data.newsletters
.filter((elem: NewsletterViewModel) => this.newsletters.findIndex((m) => m.id == elem.id) == -1)
.map((elem: NewsletterViewModel, index: number): NewsletterViewModel & { tab_pos: number } => {
return {
...elem,
tab_pos: index + offset,
};
})
.forEach((elem: NewsletterViewModel & { tab_pos: number }) => {
this.newsletters.push(elem);
});
this.loading = "fetched";
})
.catch((err) => {
this.loading = "failed";
});
},
fetchNewsletterByActiveId() {
this.loadingActive = "loading";
http
.get(`/admin/newsletter/${this.activeNewsletter}`)
.then((res) => {
this.origin = res.data;
this.activeNewsletterObj = cloneDeep(this.origin);
this.loadingActive = "fetched";
})
.catch((err) => {
this.loadingActive = "failed";
});
},
fetchNewsletterById(id: number) {
return http.get(`/admin/newsletter/${id}`);
},
async createNewsletter(newsletter: CreateNewsletterViewModel): Promise<AxiosResponse<any, any>> {
const result = await http.post(`/admin/newsletter`, {
title: newsletter.title,
});
this.fetchNewsletters();
return result;
},
async synchronizeActiveNewsletter(): Promise<void> {
if (this.origin == null || this.activeNewsletterObj == null) return;
this.syncingNewsletter = "syncing";
await http
.patch(`/admin/newsletter/${this.origin.id}/synchronize`, {
title: this.activeNewsletterObj.title,
description: this.activeNewsletterObj.description,
newsletterTitle: this.activeNewsletterObj.newsletterTitle,
newsletterText: this.activeNewsletterObj.newsletterText,
newsletterSignatur: this.activeNewsletterObj.newsletterSignatur,
recipientsByQueryId: this.activeNewsletterObj.recipientsByQueryId,
})
.then((res) => {
this.syncingNewsletter = "synced";
})
.catch((err) => {
this.syncingNewsletter = "failed";
});
this.fetchNewsletterById(this.origin.id)
.then((res) => {
this.origin = res.data;
if (this.detectedChangeNewsletter) this.syncingNewsletter = "detectedChanges";
})
.catch((err) => {});
},
},
});

View file

@ -0,0 +1,64 @@
import { defineStore } from "pinia";
import { http } from "@/serverCom";
import type { NewsletterDatesViewModel, SyncNewsletterDatesViewModel } from "@/viewmodels/admin/newsletterDates.models";
import { useProtocolStore } from "./protocol";
import cloneDeep from "lodash.clonedeep";
import isEqual from "lodash.isequal";
import differenceWith from "lodash.differencewith";
export const useNewsletterDatesStore = defineStore("newsletterDates", {
state: () => {
return {
dates: [] as Array<NewsletterDatesViewModel>,
origin: [] as Array<NewsletterDatesViewModel>,
loading: "loading" as "loading" | "fetched" | "failed",
syncingNewsletterDates: "synced" as "synced" | "syncing" | "detectedChanges" | "failed",
};
},
getters: {
detectedChangeNewsletterDates: (state) =>
!isEqual(state.origin, state.dates) && state.syncingNewsletterDates != "syncing",
},
actions: {
setNewsletterDatesSyncingState(state: "synced" | "syncing" | "detectedChanges" | "failed") {
this.syncingNewsletterDates = state;
},
fetchNewsletterDates() {
this.loading = "loading";
this.fetchNewsletterDatesPromise()
.then((result) => {
this.origin = result.data;
this.dates = cloneDeep(this.origin);
this.loading = "fetched";
})
.catch((err) => {
this.loading = "failed";
});
},
fetchNewsletterDatesPromise() {
const protocolId = useProtocolStore().activeProtocol;
return http.get(`/admin/protocol/${protocolId}/agenda`);
},
async synchronizeActiveNewsletterDates() {
this.syncingNewsletterDates = "syncing";
const protocolId = useProtocolStore().activeProtocol;
await http
.patch(`/admin/protocol/${protocolId}/synchronize/agenda`, {
agenda: differenceWith(this.dates, this.origin, isEqual),
})
.then((res) => {
this.syncingNewsletterDates = "synced";
})
.catch((err) => {
this.syncingNewsletterDates = "failed";
});
this.fetchNewsletterDatesPromise()
.then((res) => {
this.origin = res.data;
if (this.detectedChangeNewsletterDates) this.syncingNewsletterDates = "detectedChanges";
})
.catch((err) => {});
},
},
});

View file

@ -0,0 +1,65 @@
import { defineStore } from "pinia";
import { http } from "@/serverCom";
import type {
NewsletterRecipientsViewModel,
SyncNewsletterRecipientsViewModel,
} from "@/viewmodels/admin/newsletterRecipients.models";
import { useProtocolStore } from "./protocol";
import cloneDeep from "lodash.clonedeep";
import isEqual from "lodash.isequal";
export const useNewsletterRecipientsStore = defineStore("newsletterRecipients", {
state: () => {
return {
recipients: [] as Array<number>,
origin: [] as Array<number>,
loading: "loading" as "loading" | "fetched" | "failed",
syncingNewsletterRecipients: "synced" as "synced" | "syncing" | "detectedChanges" | "failed",
};
},
getters: {
detectedChangeNewsletterRecipients: (state) =>
!isEqual(state.origin, state.recipients) && state.syncingNewsletterRecipients != "syncing",
},
actions: {
setNewsletterRecipientsSyncingState(state: "synced" | "syncing" | "detectedChanges" | "failed") {
this.syncingNewsletterRecipients = state;
},
fetchNewsletterRecipients() {
this.loading = "loading";
this.fetchNewsletterRecipientsPromise()
.then((result) => {
this.origin = result.data.map((d: NewsletterRecipientsViewModel) => d.memberId);
this.recipients = cloneDeep(this.origin);
this.loading = "fetched";
})
.catch((err) => {
this.loading = "failed";
});
},
fetchNewsletterRecipientsPromise() {
const protocolId = useProtocolStore().activeProtocol;
return http.get(`/admin/protocol/${protocolId}/presence`);
},
async synchronizeActiveNewsletterRecipients() {
this.syncingNewsletterRecipients = "syncing";
const protocolId = useProtocolStore().activeProtocol;
await http
.put(`/admin/protocol/${protocolId}/synchronize/presence`, {
presence: this.recipients,
})
.then((res) => {
this.syncingNewsletterRecipients = "synced";
})
.catch((err) => {
this.syncingNewsletterRecipients = "failed";
});
this.fetchNewsletterRecipientsPromise()
.then((result) => {
this.origin = result.data.map((d: NewsletterRecipientsViewModel) => d.memberId);
if (this.detectedChangeNewsletterRecipients) this.syncingNewsletterRecipients = "detectedChanges";
})
.catch((err) => {});
},
},
});

View file

@ -1,9 +1,6 @@
import { defineStore } from "pinia";
import { http } from "@/serverCom";
import type {
ProtocolAgendaViewModel,
SyncProtocolAgendaViewModel,
} from "../../viewmodels/admin/protocolAgenda.models";
import type { ProtocolAgendaViewModel, SyncProtocolAgendaViewModel } from "@/viewmodels/admin/protocolAgenda.models";
import { useProtocolStore } from "./protocol";
import cloneDeep from "lodash.clonedeep";
import isEqual from "lodash.isequal";

View file

@ -4,7 +4,7 @@ import type { AxiosResponse } from "axios";
import type {
ProtocolDecisionViewModel,
SyncProtocolDecisionViewModel,
} from "../../viewmodels/admin/protocolDecision.models";
} from "@/viewmodels/admin/protocolDecision.models";
import { useProtocolStore } from "./protocol";
import cloneDeep from "lodash.clonedeep";
import isEqual from "lodash.isequal";

View file

@ -4,7 +4,7 @@ import type { AxiosResponse } from "axios";
import type {
ProtocolPresenceViewModel,
SyncProtocolPresenceViewModel,
} from "../../viewmodels/admin/protocolPresence.models";
} from "@/viewmodels/admin/protocolPresence.models";
import { useProtocolStore } from "./protocol";
import cloneDeep from "lodash.clonedeep";
import isEqual from "lodash.isequal";

View file

@ -1,6 +1,6 @@
import { defineStore } from "pinia";
import { http } from "@/serverCom";
import type { ProtocolPrintoutViewModel } from "../../viewmodels/admin/protocolPrintout.models";
import type { ProtocolPrintoutViewModel } from "@/viewmodels/admin/protocolPrintout.models";
import { useProtocolStore } from "./protocol";
import type { AxiosResponse } from "axios";

View file

@ -1,10 +1,7 @@
import { defineStore } from "pinia";
import { http } from "@/serverCom";
import type { AxiosResponse } from "axios";
import type {
ProtocolVotingViewModel,
SyncProtocolVotingViewModel,
} from "../../viewmodels/admin/protocolVoting.models";
import type { ProtocolVotingViewModel, SyncProtocolVotingViewModel } from "@/viewmodels/admin/protocolVoting.models";
import { useProtocolStore } from "./protocol";
import cloneDeep from "lodash.clonedeep";
import isEqual from "lodash.isequal";

View file

@ -1,8 +1,8 @@
import { defineStore } from "pinia";
import { http } from "@/serverCom";
import type { TableMeta } from "../../viewmodels/admin/query.models";
import type { DynamicQueryStructure, FieldType } from "../../types/dynamicQueries";
import { flattenQueryResult } from "../../helpers/queryFormatter";
import type { TableMeta } from "@/viewmodels/admin/query.models";
import type { DynamicQueryStructure, FieldType } from "@/types/dynamicQueries";
import { flattenQueryResult } from "@/helpers/queryFormatter";
export const useQueryBuilderStore = defineStore("queryBuilder", {
state: () => {

View file

@ -5,7 +5,7 @@ import type {
CreateTemplateViewModel,
TemplateViewModel,
UpdateTemplateViewModel,
} from "../../viewmodels/admin/template.models";
} from "@/viewmodels/admin/template.models";
export const useTemplateStore = defineStore("template", {
state: () => {

View file

@ -1,9 +1,9 @@
import { defineStore } from "pinia";
import { http } from "@/serverCom";
import type { AxiosResponse } from "axios";
import type { CreateTemplateViewModel, UpdateTemplateViewModel } from "../../viewmodels/admin/template.models";
import type { TemplateUsageViewModel, UpdateTemplateUsageViewModel } from "../../viewmodels/admin/templateUsage.models";
import type { PermissionModule } from "../../types/permissionTypes";
import type { CreateTemplateViewModel, UpdateTemplateViewModel } from "@/viewmodels/admin/template.models";
import type { TemplateUsageViewModel, UpdateTemplateUsageViewModel } from "@/viewmodels/admin/templateUsage.models";
import type { PermissionModule } from "@/types/permissionTypes";
export const useTemplateUsageStore = defineStore("templateUsage", {
state: () => {