69 lines
1.7 KiB
TypeScript
69 lines
1.7 KiB
TypeScript
"use server"
|
|
|
|
import type { Position } from "@/types"
|
|
import { cookies } from "next/headers"
|
|
|
|
export async function getMyPositions(): Promise<Position[]> {
|
|
const store = await cookies()
|
|
const accessToken = store.get("access_token")?.value
|
|
if (!accessToken) return []
|
|
|
|
const response = await fetch(
|
|
`${process.env.KOTOSOFT_ID_API_URL}/api/v1/me/positions`,
|
|
{
|
|
headers: {
|
|
Authorization: `Bearer ${accessToken}`,
|
|
},
|
|
cache: "no-store",
|
|
}
|
|
)
|
|
|
|
if (!response.ok) return []
|
|
return (await response.json()) as Position[]
|
|
}
|
|
|
|
export async function listAllPositions(): Promise<Position[] | null> {
|
|
const store = await cookies()
|
|
const accessToken = store.get("access_token")?.value
|
|
if (!accessToken) return null
|
|
|
|
const response = await fetch(
|
|
`${process.env.KOTOSOFT_ID_API_URL}/api/v1/admin/positions`,
|
|
{
|
|
headers: {
|
|
Authorization: `Bearer ${accessToken}`,
|
|
},
|
|
cache: "no-store",
|
|
}
|
|
)
|
|
|
|
if (response.status === 403) return null
|
|
if (!response.ok) return null
|
|
return (await response.json()) as Position[]
|
|
}
|
|
|
|
export async function upsertPosition(code: string, name: string, description?: string | null): Promise<{ success: boolean }> {
|
|
const store = await cookies()
|
|
const accessToken = store.get("access_token")?.value
|
|
if (!accessToken) return { success: false }
|
|
|
|
const response = await fetch(
|
|
`${process.env.KOTOSOFT_ID_API_URL}/api/v1/admin/positions`,
|
|
{
|
|
method: "POST",
|
|
headers: {
|
|
Authorization: `Bearer ${accessToken}`,
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({ code, name, ...(description ? { description } : {}) }),
|
|
}
|
|
)
|
|
|
|
if (!response.ok) {
|
|
return { success: false }
|
|
}
|
|
|
|
return { success: true }
|
|
}
|
|
|