enhance: use QueryStoreId to fetch query

This commit is contained in:
Julian Krauser 2025-04-19 10:05:01 +02:00
parent 916e61897a
commit a20c0d3ed3
3 changed files with 23 additions and 21 deletions

View file

@ -2,6 +2,7 @@ import { defineStore } from "pinia";
import { http } from "@/serverCom"; import { http } from "@/serverCom";
import type { TableMeta } from "@/viewmodels/admin/configuration/query.models"; import type { TableMeta } from "@/viewmodels/admin/configuration/query.models";
import type { DynamicQueryStructure, FieldType } from "@/types/dynamicQueries"; import type { DynamicQueryStructure, FieldType } from "@/types/dynamicQueries";
import type { AxiosResponse } from "axios";
export const useQueryBuilderStore = defineStore("queryBuilder", { export const useQueryBuilderStore = defineStore("queryBuilder", {
state: () => { state: () => {
@ -58,6 +59,16 @@ export const useQueryBuilderStore = defineStore("queryBuilder", {
this.loadingData = "failed"; this.loadingData = "failed";
}); });
}, },
async sendQueryByStoreId(
id: string,
offset = 0,
count = 25,
noLimit: boolean = false
): Promise<AxiosResponse<any, any>> {
return await http.post(
`/admin/querybuilder/query/${id}?` + (noLimit ? `noLimit=true` : `offset=${offset}&count=${count}`)
);
},
clearResults() { clearResults() {
this.data = []; this.data = [];
this.totalLength = 0; this.totalLength = 0;

View file

@ -1,5 +1,3 @@
import type { QueryViewModel } from "../../configuration/query.models";
export interface NewsletterViewModel { export interface NewsletterViewModel {
id: number; id: number;
title: string; title: string;
@ -9,7 +7,6 @@ export interface NewsletterViewModel {
newsletterSignatur: string; newsletterSignatur: string;
isSent: boolean; isSent: boolean;
recipientsByQueryId?: string | null; recipientsByQueryId?: string | null;
recipientsByQuery?: QueryViewModel | null;
} }
export interface CreateNewsletterViewModel { export interface CreateNewsletterViewModel {

View file

@ -81,6 +81,7 @@ import { useQueryStoreStore } from "@/stores/admin/configuration/queryStore";
import { useQueryBuilderStore } from "@/stores/admin/club/queryBuilder"; import { useQueryBuilderStore } from "@/stores/admin/club/queryBuilder";
import cloneDeep from "lodash.clonedeep"; import cloneDeep from "lodash.clonedeep";
import MemberSearchSelect from "@/components/admin/MemberSearchSelect.vue"; import MemberSearchSelect from "@/components/admin/MemberSearchSelect.vue";
import type { FieldType } from "@/types/dynamicQueries";
</script> </script>
<script lang="ts"> <script lang="ts">
@ -88,14 +89,9 @@ export default defineComponent({
props: { props: {
newsletterId: String, newsletterId: String,
}, },
watch: {
recipientsByQuery() {
this.loadQuery();
},
},
data() { data() {
return { return {
query: "" as String, queryResult: [] as Array<{ id: FieldType; [key: string]: FieldType }>,
members: [] as Array<MemberViewModel>, members: [] as Array<MemberViewModel>,
showMemberSelect: false as boolean, showMemberSelect: false as boolean,
}; };
@ -104,7 +100,6 @@ export default defineComponent({
...mapWritableState(useNewsletterRecipientsStore, ["recipients", "loading"]), ...mapWritableState(useNewsletterRecipientsStore, ["recipients", "loading"]),
...mapWritableState(useNewsletterStore, ["activeNewsletterObj"]), ...mapWritableState(useNewsletterStore, ["activeNewsletterObj"]),
...mapState(useQueryStoreStore, ["queries"]), ...mapState(useQueryStoreStore, ["queries"]),
...mapState(useQueryBuilderStore, ["data"]),
...mapState(useAbilityStore, ["can"]), ...mapState(useAbilityStore, ["can"]),
selected(): Array<MemberViewModel> { selected(): Array<MemberViewModel> {
return this.members return this.members
@ -119,10 +114,10 @@ export default defineComponent({
}, },
queried(): Array<MemberViewModel> { queried(): Array<MemberViewModel> {
if (this.recipientsByQueryId == "def") return []; if (this.recipientsByQueryId == "def") return [];
let keys = Object.keys(this.data?.[0] ?? {}); let keys = Object.keys(this.queryResult?.[0] ?? {});
let memberKey = keys.find((k) => k.includes("member_id")); let memberKey = keys.find((k) => k.includes("member_id"));
return this.members.filter((m) => return this.members.filter((m) =>
this.data this.queryResult
.map((t) => ({ .map((t) => ({
id: t.id, id: t.id,
...(memberKey ? { memberId: t[memberKey] } : {}), ...(memberKey ? { memberId: t[memberKey] } : {}),
@ -149,17 +144,12 @@ export default defineComponent({
if (this.activeNewsletterObj == undefined) return; if (this.activeNewsletterObj == undefined) return;
if (val == "def") { if (val == "def") {
this.activeNewsletterObj.recipientsByQueryId = null; this.activeNewsletterObj.recipientsByQueryId = null;
this.activeNewsletterObj.recipientsByQuery = null;
} else if (this.queries.find((q) => q.id == val)) { } else if (this.queries.find((q) => q.id == val)) {
this.activeNewsletterObj.recipientsByQueryId = val; this.activeNewsletterObj.recipientsByQueryId = val;
this.activeNewsletterObj.recipientsByQuery = cloneDeep(this.queries.find((q) => q.id == val)); this.loadQuery();
this.sendQuery(0, 0, this.recipientsByQuery?.query, true);
} }
}, },
}, },
recipientsByQuery() {
return this.activeNewsletterObj?.recipientsByQuery;
},
}, },
mounted() { mounted() {
// this.fetchNewsletterRecipients(); // this.fetchNewsletterRecipients();
@ -171,7 +161,7 @@ export default defineComponent({
...mapActions(useMemberStore, ["getAllMembers"]), ...mapActions(useMemberStore, ["getAllMembers"]),
...mapActions(useNewsletterRecipientsStore, ["fetchNewsletterRecipients"]), ...mapActions(useNewsletterRecipientsStore, ["fetchNewsletterRecipients"]),
...mapActions(useQueryStoreStore, ["fetchQueries"]), ...mapActions(useQueryStoreStore, ["fetchQueries"]),
...mapActions(useQueryBuilderStore, ["sendQuery"]), ...mapActions(useQueryBuilderStore, ["sendQueryByStoreId"]),
removeSelected(id: string) { removeSelected(id: string) {
let index = this.recipients.findIndex((s) => s == id); let index = this.recipients.findIndex((s) => s == id);
if (index != -1) { if (index != -1) {
@ -186,8 +176,12 @@ export default defineComponent({
.catch(() => {}); .catch(() => {});
}, },
loadQuery() { loadQuery() {
if (this.recipientsByQuery) { if (this.recipientsByQueryId != "def") {
this.sendQuery(0, 0, this.recipientsByQuery.query, true); this.sendQueryByStoreId(this.recipientsByQueryId, 0, 0, true)
.then((result) => {
this.queryResult = result.data.rows;
})
.catch(() => {});
} }
}, },
}, },