link-stack/apps/link/app/_actions/tickets.ts

195 lines
4.3 KiB
TypeScript
Raw Normal View History

2024-05-09 07:42:44 +02:00
"use server";
import { revalidatePath } from "next/cache";
import { getTicketQuery } from "app/_graphql/getTicketQuery";
import { getTicketArticlesQuery } from "app/_graphql/getTicketArticlesQuery";
2024-05-09 07:42:44 +02:00
import { createTicketMutation } from "app/_graphql/createTicketMutation";
import { updateTicketMutation } from "app/_graphql/updateTicketMutation";
import { updateTagsMutation } from "app/_graphql/updateTagsMutation";
import { executeGraphQL, executeREST } from "app/_lib/zammad";
2024-05-09 07:42:44 +02:00
export const createTicketAction = async (
currentState: any,
formData: FormData,
) => {
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"),
},
};
2024-05-09 07:42:44 +02:00
console.log({ ticket });
const result = await executeGraphQL({
query: createTicketMutation,
2024-05-09 07:42:44 +02:00
variables: {
input: ticket,
2024-05-09 07:42:44 +02:00
},
});
2024-05-14 09:40:58 +02:00
console.log({ result });
return {
...currentState,
values: ticket,
success: true,
};
} catch (e: any) {
console.log({ e });
return {
success: false,
values: {},
message: e?.message ?? "Unknown error",
2024-05-09 07:42:44 +02:00
};
}
};
2024-05-09 07:42:44 +02:00
export const createTicketArticleAction = async (
ticketID: string,
article: Record<string, any>,
) => {
try {
const result = await executeGraphQL({
query: updateTicketMutation,
2024-05-09 07:42:44 +02:00
variables: {
ticketId: `gid://zammad/Ticket/${ticketID}`,
input: { article },
2024-05-09 07:42:44 +02:00
},
});
console.log({ result });
2024-05-09 07:42:44 +02:00
return {
result,
2024-05-09 07:42:44 +02:00
success: true,
};
} catch (e: any) {
console.log({ e });
return {
success: false,
message: e?.message ?? "Unknown error",
};
2024-05-09 07:42:44 +02:00
}
};
export const updateTicketAction = async (
currentState: any,
formData: FormData,
) => {
2024-05-14 09:40:58 +02:00
/*
2024-05-09 07:42:44 +02:00
try {
const { id, project } = currentState.values;
const updatedTicket = {
title: formData.get("title"),
};
await executeMutation({
project,
mutation: UpdateAssetMutation,
variables: {
id,
input: updatedAsset,
},
});
revalidatePath(`/${project}/assets/${id}`);
return {
...currentState,
values: { ...currentState.values, ...updatedAsset, id, project },
success: true,
};
} catch (e: any) {
return { success: false, message: e?.message ?? "Unknown error" };
}
2024-05-14 09:40:58 +02:00
*/
2024-05-09 07:42:44 +02:00
};
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;
};
2024-05-09 07:42:44 +02:00
export const updateTicketTagsAction = async (
currentState: any,
formData: FormData,
) => {
2024-05-14 09:40:58 +02:00
/*
2024-05-09 07:42:44 +02:00
try {
const { id, project } = currentState.values;
const updatedTicket = {
title: formData.get("title"),
};
await executeMutation({
project,
mutation: UpdateAssetMutation,
variables: {
id,
input: updatedAsset,
},
});
revalidatePath(`/${project}/assets/${id}`);
return {
...currentState,
values: { ...currentState.values, ...updatedAsset, id, project },
success: true,
};
} catch (e: any) {
return { success: false, message: e?.message ?? "Unknown error" };
}
2024-05-14 09:40:58 +02:00
*/
2024-05-09 07:42:44 +02:00
};
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",
});
2024-08-06 08:36:03 +02:00
const formattedStates =
states?.map((state: any) => ({
value: state.id,
label: state.name,
})) ?? [];
return formattedStates;
};
export const getTicketPrioritiesAction = async () => {
const priorities = await executeREST({
path: "/api/v1/ticket_priorities",
});
2024-08-06 08:36:03 +02:00
const formattedPriorities =
priorities?.map((priority: any) => ({
value: priority.id,
label: priority.name,
})) ?? [];
return formattedPriorities;
};