2024-05-09 07:42:44 +02:00
|
|
|
"use server";
|
|
|
|
|
|
2024-08-05 23:31:15 +02:00
|
|
|
import { executeREST } from "app/_lib/zammad";
|
2025-08-20 11:37:39 +02:00
|
|
|
import { createLogger } from "@link-stack/logger";
|
|
|
|
|
|
|
|
|
|
const logger = createLogger('link-users');
|
2024-08-05 23:31:15 +02:00
|
|
|
|
2024-11-25 11:48:19 +01:00
|
|
|
export const getAgentsAction = async (groupID: number) => {
|
2024-08-14 10:51:12 +02:00
|
|
|
try {
|
2024-11-25 11:48:19 +01:00
|
|
|
const group = await executeREST({
|
|
|
|
|
path: `/api/v1/groups/${groupID}`,
|
2024-08-14 10:51:12 +02:00
|
|
|
});
|
2024-11-25 11:48:19 +01:00
|
|
|
const { user_ids: groupUserIDs } = group;
|
|
|
|
|
const path = `/api/v1/users/search?query=role_ids:2&limit=1000`;
|
|
|
|
|
const users = await executeREST({ path });
|
2024-08-14 10:51:12 +02:00
|
|
|
const agents =
|
2024-11-25 11:48:19 +01:00
|
|
|
users?.filter((user: any) => groupUserIDs.includes(user.id)) ?? [];
|
2024-08-14 10:51:12 +02:00
|
|
|
const formattedAgents = agents
|
|
|
|
|
.map((agent: any) => ({
|
|
|
|
|
label: `${agent.firstname} ${agent.lastname}`,
|
|
|
|
|
value: `gid://zammad/User/${agent.id}`,
|
|
|
|
|
}))
|
|
|
|
|
.sort((a: any, b: any) => a.label.localeCompare(b.label));
|
2024-08-05 23:31:15 +02:00
|
|
|
|
2024-08-14 10:51:12 +02:00
|
|
|
return formattedAgents;
|
|
|
|
|
} catch (e) {
|
2025-08-20 11:37:39 +02:00
|
|
|
logger.error({ error: e }, "Error occurred");
|
2024-08-14 10:51:12 +02:00
|
|
|
return [];
|
|
|
|
|
}
|
2024-08-05 23:31:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getCustomersAction = async () => {
|
2024-08-14 10:51:12 +02:00
|
|
|
try {
|
|
|
|
|
const users = await executeREST({
|
|
|
|
|
path: "/api/v1/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));
|
2024-08-05 23:31:15 +02:00
|
|
|
|
2024-08-14 10:51:12 +02:00
|
|
|
return formattedCustomers;
|
|
|
|
|
} catch (e) {
|
2025-08-20 11:37:39 +02:00
|
|
|
logger.error({ error: e }, "Error occurred");
|
2024-08-14 10:51:12 +02:00
|
|
|
return [];
|
|
|
|
|
}
|
2024-08-05 23:31:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getUsersAction = async () => {
|
2024-08-14 10:51:12 +02:00
|
|
|
try {
|
|
|
|
|
const users = await executeREST({
|
|
|
|
|
path: "/api/v1/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));
|
2024-08-05 23:31:15 +02:00
|
|
|
|
2024-08-14 10:51:12 +02:00
|
|
|
return formattedUsers;
|
|
|
|
|
} catch (e) {
|
2025-08-20 11:37:39 +02:00
|
|
|
logger.error({ error: e }, "Error occurred");
|
2024-08-14 10:51:12 +02:00
|
|
|
return [];
|
|
|
|
|
}
|
2024-08-05 23:31:15 +02:00
|
|
|
};
|