link-stack/apps/link/app/_actions/users.ts
2024-11-25 11:48:19 +01:00

67 lines
1.8 KiB
TypeScript

"use server";
import { executeREST } from "app/_lib/zammad";
export const getAgentsAction = async (groupID: number) => {
try {
const group = await executeREST({
path: `/api/v1/groups/${groupID}`,
});
const { user_ids: groupUserIDs } = group;
const path = `/api/v1/users/search?query=role_ids:2&limit=1000`;
const users = await executeREST({ path });
const agents =
users?.filter((user: any) => groupUserIDs.includes(user.id)) ?? [];
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));
return formattedAgents;
} catch (e) {
console.error(e.message);
return [];
}
};
export const getCustomersAction = async () => {
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));
return formattedCustomers;
} catch (e) {
console.error(e.message);
return [];
}
};
export const getUsersAction = async () => {
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));
return formattedUsers;
} catch (e) {
console.error(e.message);
return [];
}
};