new features
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"use server"
|
||||
|
||||
import type { Permission, Position } from "@/types"
|
||||
import type { AdminUser, Permission, Position } from "@/types"
|
||||
import { cookies } from "next/headers"
|
||||
|
||||
async function getAccessToken(): Promise<string | null> {
|
||||
@@ -12,9 +12,9 @@ async function getAuthHeaders(locale?: string): Promise<HeadersInit> {
|
||||
const token = await getAccessToken()
|
||||
return token
|
||||
? {
|
||||
Authorization: `Bearer ${token}`,
|
||||
"Accept-Language": locale ?? "en",
|
||||
}
|
||||
Authorization: `Bearer ${token}`,
|
||||
"Accept-Language": locale ?? "en",
|
||||
}
|
||||
: {}
|
||||
}
|
||||
|
||||
@@ -70,38 +70,51 @@ export async function savePosition(
|
||||
return handleSuccessResponse(res)
|
||||
}
|
||||
|
||||
export async function fetchAllPermissions(): Promise<Permission[]> {
|
||||
export async function fetchAllPermissions(locale?: string): Promise<Permission[]> {
|
||||
const token = await getAccessToken()
|
||||
if (!token) return []
|
||||
|
||||
const res = await fetch(`${process.env.KOTOSOFT_ID_API_URL}/api/v1/admin/permissions`, {
|
||||
headers: await getAuthHeaders(),
|
||||
headers: await getAuthHeaders(locale),
|
||||
cache: "no-store",
|
||||
})
|
||||
|
||||
return (await handleJsonResponse<Permission[]>(res)) ?? []
|
||||
}
|
||||
|
||||
export async function addPermission(code: string, description?: string): Promise<{ success: boolean }> {
|
||||
export async function fetchAdminUsers(locale?: string): Promise<AdminUser[] | null> {
|
||||
const token = await getAccessToken()
|
||||
if (!token) return null
|
||||
|
||||
const res = await fetch(`${process.env.KOTOSOFT_ID_API_URL}/api/v1/admin/users`, {
|
||||
headers: await getAuthHeaders(locale),
|
||||
cache: "no-store",
|
||||
})
|
||||
|
||||
if (res.status === 403) return []
|
||||
return await handleJsonResponse<AdminUser[]>(res)
|
||||
}
|
||||
|
||||
export async function addPermission(code: string, description?: string, locale?: string): Promise<{ success: boolean }> {
|
||||
const token = await getAccessToken()
|
||||
if (!token) return { success: false }
|
||||
|
||||
const res = await fetch(`${process.env.KOTOSOFT_ID_API_URL}/api/v1/admin/permissions`, {
|
||||
method: "POST",
|
||||
headers: { ...(await getAuthHeaders()), "Content-Type": "application/json" },
|
||||
headers: { ...(await getAuthHeaders(locale)), "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ code, ...(description ? { description } : {}) }),
|
||||
})
|
||||
|
||||
return handleSuccessResponse(res)
|
||||
}
|
||||
|
||||
export async function editPermission(code: string, description?: string): Promise<{ success: boolean }> {
|
||||
export async function editPermission(code: string, description?: string, locale?: string): Promise<{ success: boolean }> {
|
||||
const token = await getAccessToken()
|
||||
if (!token) return { success: false }
|
||||
|
||||
const res = await fetch(`${process.env.KOTOSOFT_ID_API_URL}/api/v1/admin/permissions/${code}`, {
|
||||
method: "PUT",
|
||||
headers: { ...(await getAuthHeaders()), "Content-Type": "application/json" },
|
||||
headers: { ...(await getAuthHeaders(locale)), "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ ...(description ? { description } : {}) }),
|
||||
})
|
||||
|
||||
@@ -120,6 +133,29 @@ export async function removePermission(code: string): Promise<{ success: boolean
|
||||
return handleSuccessResponse(res)
|
||||
}
|
||||
|
||||
function diffPermissions(oldP: string[], newP: string[]) {
|
||||
const oldSet = new Set(oldP)
|
||||
const newSet = new Set(newP)
|
||||
|
||||
const toAdd = newP.filter(p => !oldSet.has(p))
|
||||
const toRemove = oldP.filter(p => !newSet.has(p))
|
||||
|
||||
return { toAdd, toRemove }
|
||||
}
|
||||
|
||||
export async function syncPositionPermissions(
|
||||
positionCode: string,
|
||||
oldPermissions: string[],
|
||||
newPermissions: string[]
|
||||
) {
|
||||
const { toAdd, toRemove } = diffPermissions(oldPermissions, newPermissions)
|
||||
|
||||
await Promise.all([
|
||||
...toAdd.map(p => attachPermissionToPosition(positionCode, p)),
|
||||
...toRemove.map(p => detachPermissionFromPosition(positionCode, p)),
|
||||
])
|
||||
}
|
||||
|
||||
export async function attachPermissionToPosition(
|
||||
positionCode: string,
|
||||
permissionCode: string
|
||||
@@ -191,26 +227,26 @@ export async function revokePositionFromUser(userId: string, positionCode: strin
|
||||
return handleSuccessResponse(res)
|
||||
}
|
||||
|
||||
export async function addPosition(code: string, name: string, description?: string): Promise<{ success: boolean }> {
|
||||
export async function addPosition(code: string, name: string, description?: string, locale?: string): Promise<{ success: boolean }> {
|
||||
const token = await getAccessToken()
|
||||
if (!token) return { success: false }
|
||||
|
||||
const res = await fetch(`${process.env.KOTOSOFT_ID_API_URL}/api/v1/admin/positions`, {
|
||||
method: "POST",
|
||||
headers: { ...(await getAuthHeaders()), "Content-Type": "application/json" },
|
||||
headers: { ...(await getAuthHeaders(locale)), "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ code, name, ...(description ? { description } : {}) }),
|
||||
})
|
||||
|
||||
return handleSuccessResponse(res)
|
||||
}
|
||||
|
||||
export async function editPosition(code: string, name: string, description?: string): Promise<{ success: boolean }> {
|
||||
export async function editPosition(code: string, name: string, description?: string, locale?: string): Promise<{ success: boolean }> {
|
||||
const token = await getAccessToken()
|
||||
if (!token) return { success: false }
|
||||
|
||||
const res = await fetch(`${process.env.KOTOSOFT_ID_API_URL}/api/v1/admin/positions/${code}`, {
|
||||
method: "PUT",
|
||||
headers: { ...(await getAuthHeaders()), "Content-Type": "application/json" },
|
||||
headers: { ...(await getAuthHeaders(locale)), "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name, ...(description ? { description } : {}) }),
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user