Use server actions instead of client-side API calls
This commit is contained in:
parent
5a3127dcb0
commit
aa453954ed
30 changed files with 703 additions and 462 deletions
16
apps/link/app/_actions/groups.ts
Normal file
16
apps/link/app/_actions/groups.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
"use server";
|
||||
|
||||
import { executeREST } from "app/_lib/zammad";
|
||||
|
||||
export const getGroupsAction = async () => {
|
||||
const groups = await executeREST({
|
||||
path: "/api/v1/groups",
|
||||
});
|
||||
const allGroups = groups ?? [];
|
||||
const formattedGroups = allGroups.map((group: any) => ({
|
||||
label: group.name,
|
||||
value: `gid://zammad/Group/${group.id}`,
|
||||
}));
|
||||
|
||||
return formattedGroups;
|
||||
};
|
||||
82
apps/link/app/_actions/overviews.ts
Normal file
82
apps/link/app/_actions/overviews.ts
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
"use server";
|
||||
|
||||
import { executeGraphQL } from "app/_lib/zammad";
|
||||
import { getTicketOverviewCountsQuery } from "app/_graphql/getTicketOverviewCountsQuery";
|
||||
import { getTicketsByOverviewQuery } from "app/_graphql/getTicketsByOverviewQuery";
|
||||
|
||||
export const getOverviewTicketCountsAction = async () => {
|
||||
const countResult = await executeGraphQL({
|
||||
query: getTicketOverviewCountsQuery,
|
||||
});
|
||||
const overviews = countResult?.ticketOverviews?.edges;
|
||||
const counts = overviews?.reduce((acc: any, overview: any) => {
|
||||
acc[overview.node.name] = overview.node.ticketCount;
|
||||
return acc;
|
||||
});
|
||||
|
||||
return counts;
|
||||
};
|
||||
|
||||
export const getOverviewTicketsAction = async (name: string) => {
|
||||
let tickets = [];
|
||||
let error = null;
|
||||
|
||||
if (name === "Recent") {
|
||||
const response = await fetch(
|
||||
`${process.env.ZAMMAD_URL}/api/v1/recent_view`,
|
||||
);
|
||||
const recent = await response.json();
|
||||
console.log({ recent });
|
||||
|
||||
for (const rec of recent) {
|
||||
const res = await fetch(
|
||||
`${process.env.ZAMMAD_URL}/api/v1/tickets/${rec.o_id}`,
|
||||
);
|
||||
const tkt = await res.json();
|
||||
tickets.push({
|
||||
...tkt,
|
||||
internalId: tkt.id,
|
||||
createdAt: tkt.created_at,
|
||||
updatedAt: tkt.updated_at,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
const overviewLookup = {
|
||||
Assigned: "My Assigned Tickets",
|
||||
Open: "Open Tickets",
|
||||
Urgent: "Escalated Tickets",
|
||||
Unassigned: "Unassigned & Open Tickets",
|
||||
};
|
||||
const fullName = overviewLookup[name];
|
||||
const countResult = await executeGraphQL({
|
||||
query: getTicketOverviewCountsQuery,
|
||||
});
|
||||
const overviewID = countResult?.ticketOverviews?.edges?.find(
|
||||
(overview: any) => overview.node.name === fullName,
|
||||
)?.node?.id;
|
||||
|
||||
console.log({ overviewID });
|
||||
|
||||
const ticketsResult = await executeGraphQL({
|
||||
query: getTicketsByOverviewQuery,
|
||||
variables: { overviewId: overviewID, pageSize: 250 },
|
||||
});
|
||||
|
||||
const edges = ticketsResult?.ticketsByOverview?.edges;
|
||||
if (edges) {
|
||||
tickets = edges.map((edge: any) => edge.node);
|
||||
}
|
||||
}
|
||||
|
||||
const sortedTickets = tickets.sort((a: any, b: any) => {
|
||||
if (a.internalId < b.internalId) {
|
||||
return 1;
|
||||
}
|
||||
if (a.internalId > b.internalId) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
|
||||
return { tickets: sortedTickets, error };
|
||||
};
|
||||
12
apps/link/app/_actions/search.ts
Normal file
12
apps/link/app/_actions/search.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
"use server";
|
||||
import { executeGraphQL } from "app/_lib/zammad";
|
||||
import { searchQuery } from "@/app/_graphql/searchQuery";
|
||||
|
||||
export const searchAllAction = async (query: string, limit: number) => {
|
||||
const result = await executeGraphQL({
|
||||
query: searchQuery,
|
||||
variables: { search: query, limit },
|
||||
});
|
||||
|
||||
return result?.search;
|
||||
};
|
||||
|
|
@ -1,56 +1,78 @@
|
|||
"use server";
|
||||
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { getTicketQuery } from "app/_graphql/getTicketQuery";
|
||||
import { getTicketArticlesQuery } from "app/_graphql/getTicketArticlesQuery";
|
||||
import { createTicketMutation } from "app/_graphql/createTicketMutation";
|
||||
import { updateTicketMutation } from "app/_graphql/updateTicketMutation";
|
||||
import { updateTagsMutation } from "app/_graphql/updateTagsMutation";
|
||||
// import { executeMutation } from "app/_lib/graphql";
|
||||
// import { AddAssetMutation } from "../_graphql/AddAssetMutation";
|
||||
import { executeGraphQL, executeREST } from "app/_lib/zammad";
|
||||
|
||||
export const createTicketAction = async (
|
||||
currentState: any,
|
||||
formData: FormData,
|
||||
) => {
|
||||
/*
|
||||
|
||||
const createTicket = async () => {
|
||||
await fetcher({
|
||||
document: createTicketMutation,
|
||||
variables: {
|
||||
input: {
|
||||
ticket,
|
||||
},
|
||||
},
|
||||
});
|
||||
closeDialog();
|
||||
setBody("");
|
||||
};
|
||||
|
||||
|
||||
try {
|
||||
const ticket = {
|
||||
groupId: formData.get("groupId"),
|
||||
customerId: `gid://zammad/User/3`, // { email: formData.get("customerId") },
|
||||
title: formData.get("title"),
|
||||
article: {
|
||||
internal: true,
|
||||
body: formData.get("details"),
|
||||
},
|
||||
};
|
||||
|
||||
const result = await executeMutation({
|
||||
project,
|
||||
mutation: AddAssetMutation,
|
||||
console.log({ ticket });
|
||||
|
||||
const result = await executeGraphQL({
|
||||
query: createTicketMutation,
|
||||
variables: {
|
||||
input: asset,
|
||||
input: ticket,
|
||||
},
|
||||
});
|
||||
|
||||
revalidatePath(`/${project}/assets`);
|
||||
|
||||
console.log({ result });
|
||||
return {
|
||||
...currentState,
|
||||
values: { ...asset, ...result.addAsset, project },
|
||||
values: ticket,
|
||||
success: true,
|
||||
};
|
||||
} catch (e: any) {
|
||||
return { success: false, message: e?.message ?? "Unknown error" };
|
||||
console.log({ e });
|
||||
return {
|
||||
success: false,
|
||||
values: {},
|
||||
message: e?.message ?? "Unknown error",
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
export const createTicketArticleAction = async (
|
||||
ticketID: string,
|
||||
article: Record<string, any>,
|
||||
) => {
|
||||
try {
|
||||
const result = await executeGraphQL({
|
||||
query: updateTicketMutation,
|
||||
variables: {
|
||||
ticketId: `gid://zammad/Ticket/${ticketID}`,
|
||||
input: { article },
|
||||
},
|
||||
});
|
||||
|
||||
console.log({ result });
|
||||
return {
|
||||
result,
|
||||
success: true,
|
||||
};
|
||||
} catch (e: any) {
|
||||
console.log({ e });
|
||||
return {
|
||||
success: false,
|
||||
message: e?.message ?? "Unknown error",
|
||||
};
|
||||
}
|
||||
*/
|
||||
};
|
||||
|
||||
export const updateTicketAction = async (
|
||||
|
|
@ -85,6 +107,24 @@ export const updateTicketAction = async (
|
|||
*/
|
||||
};
|
||||
|
||||
export const getTicketAction = async (id: string) => {
|
||||
const ticketData = await executeGraphQL({
|
||||
query: getTicketQuery,
|
||||
variables: { ticketId: `gid://zammad/Ticket/${id}` },
|
||||
});
|
||||
|
||||
return ticketData?.ticket;
|
||||
};
|
||||
|
||||
export const getTicketArticlesAction = async (id: string) => {
|
||||
const ticketData = await executeGraphQL({
|
||||
query: getTicketArticlesQuery,
|
||||
variables: { ticketId: `gid://zammad/Ticket/${id}` },
|
||||
});
|
||||
|
||||
return ticketData?.ticketArticles;
|
||||
};
|
||||
|
||||
export const updateTicketTagsAction = async (
|
||||
currentState: any,
|
||||
formData: FormData,
|
||||
|
|
@ -116,3 +156,27 @@ export const updateTicketTagsAction = async (
|
|||
}
|
||||
*/
|
||||
};
|
||||
|
||||
export const getTicketStatesAction = async () => {
|
||||
const states = await executeREST({
|
||||
path: "/api/v1/ticket_states",
|
||||
});
|
||||
|
||||
return states;
|
||||
};
|
||||
|
||||
export const getTicketTagsAction = async () => {
|
||||
const states = await executeREST({
|
||||
path: "/api/v1/tags",
|
||||
});
|
||||
|
||||
return states;
|
||||
};
|
||||
|
||||
export const getTicketPrioritiesAction = async () => {
|
||||
const priorities = await executeREST({
|
||||
path: "/api/v1/ticket_priorities",
|
||||
});
|
||||
|
||||
return priorities;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,3 +1,50 @@
|
|||
"use server";
|
||||
|
||||
const fetchUsersAction = async () => {};
|
||||
import { executeREST } from "app/_lib/zammad";
|
||||
|
||||
export const getAgentsAction = async () => {
|
||||
const users = await executeREST({
|
||||
path: "/api/v1/users",
|
||||
});
|
||||
const agents = users?.filter((user: any) => user.role_ids.includes(2)) ?? [];
|
||||
const formattedAgents = agents
|
||||
.map((agent: any) => ({
|
||||
label: agent.login,
|
||||
value: `gid://zammad/User/${agent.id}`,
|
||||
}))
|
||||
.sort((a: any, b: any) => a.label.localeCompare(b.label));
|
||||
|
||||
return formattedAgents;
|
||||
};
|
||||
|
||||
export const getCustomersAction = async () => {
|
||||
const users = await executeREST({
|
||||
path: "/api/v1/users",
|
||||
});
|
||||
console.log({ users });
|
||||
const customers =
|
||||
users?.filter((user: any) => user.role_ids.includes(3)) ?? [];
|
||||
const formattedCustomers = customers
|
||||
.map((customer: any) => ({
|
||||
label: customer.login,
|
||||
value: `gid://zammad/User/${customer.id}`,
|
||||
}))
|
||||
.sort((a: any, b: any) => a.label.localeCompare(b.label));
|
||||
|
||||
return formattedCustomers;
|
||||
};
|
||||
|
||||
export const getUsersAction = async () => {
|
||||
const users = await executeREST({
|
||||
path: "/api/v1/users",
|
||||
});
|
||||
console.log({ users });
|
||||
const formattedUsers = users
|
||||
.map((customer: any) => ({
|
||||
label: customer.login,
|
||||
value: `gid://zammad/User/${customer.id}`,
|
||||
}))
|
||||
.sort((a: any, b: any) => a.label.localeCompare(b.label));
|
||||
|
||||
return formattedUsers;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue