-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into feat/superadmin-user-management
- Loading branch information
Showing
21 changed files
with
1,370 additions
and
235 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import axios from "axios"; | ||
|
||
import { getApiUrl } from "../getApiUrl"; | ||
|
||
export const deleteAllNotifications = async (token: string) => { | ||
const apiUrl = await getApiUrl(); | ||
|
||
try { | ||
const response = await axios.delete( | ||
`${apiUrl}/api/v1/notifications/clear-all`, | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}, | ||
); | ||
|
||
return { | ||
data: response.data, | ||
}; | ||
} catch (error) { | ||
return axios.isAxiosError(error) && error.response | ||
? { | ||
error: | ||
error.response.data.message || "Failed to delete notifications.", | ||
status: error.response.status, | ||
} | ||
: { | ||
error: "An unexpected error occurred.", | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import axios from "axios"; | ||
|
||
import { getApiUrl } from "../getApiUrl"; | ||
|
||
interface DeleteNotificationProperties { | ||
notificationId: string; | ||
token?: string; | ||
} | ||
|
||
export const deleteNotifications = async ({ | ||
notificationId, | ||
token, | ||
}: DeleteNotificationProperties) => { | ||
const apiUrl = await getApiUrl(); | ||
|
||
try { | ||
const response = await axios.delete( | ||
`${apiUrl}/api/v1/notifications/${notificationId}`, | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}, | ||
); | ||
|
||
return { | ||
data: response.data, | ||
}; | ||
} catch (error) { | ||
return axios.isAxiosError(error) && error.response | ||
? { | ||
error: | ||
error.response.data.message || "Failed to delete notifications.", | ||
status: error.response.status, | ||
} | ||
: { | ||
error: "An unexpected error occurred.", | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
"use server"; | ||
|
||
import axios from "axios"; | ||
|
||
import { auth } from "~/lib/auth"; | ||
import { getApiUrl } from "../getApiUrl"; | ||
|
||
export const getAllNotifications = async () => { | ||
const apiUrl = await getApiUrl(); | ||
const session = await auth(); | ||
try { | ||
const response = await axios.get(`${apiUrl}/api/v1/notifications`, { | ||
headers: { | ||
Authorization: `Bearer ${session?.access_token}`, | ||
}, | ||
}); | ||
|
||
return { | ||
status: response.status, | ||
data: response.data, | ||
}; | ||
} catch (error) { | ||
return axios.isAxiosError(error) && error.response | ||
? { | ||
error: | ||
error.response.data.message || "Failed to fetch notifications.", | ||
status: error.response.status, | ||
} | ||
: { | ||
error: "An unexpected error occurred.", | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import axios from "axios"; | ||
|
||
import { getApiUrl } from "../getApiUrl"; | ||
|
||
export const markAllAsRead = async (token: string) => { | ||
const apiUrl = await getApiUrl(); | ||
|
||
try { | ||
const response = await axios.patch( | ||
`${apiUrl}/api/v1/notifications`, | ||
{ is_read: true }, | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}, | ||
); | ||
|
||
return { | ||
data: response.data, | ||
}; | ||
} catch (error) { | ||
return axios.isAxiosError(error) && error.response | ||
? { | ||
error: | ||
error.response.data.message || | ||
"Failed to mark notifications as read.", | ||
status: error.response.status, | ||
} | ||
: { | ||
error: "An unexpected error occurred.", | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import axios from "axios"; | ||
|
||
import { getApiUrl } from "../getApiUrl"; | ||
|
||
interface ReadProperties { | ||
notificationId: string; | ||
token?: string; | ||
} | ||
|
||
export const markOneAsRead = async ({ | ||
notificationId, | ||
token, | ||
}: ReadProperties) => { | ||
const apiUrl = await getApiUrl(); | ||
|
||
try { | ||
const response = await axios.patch( | ||
`${apiUrl}/api/v1/notifications/${notificationId}`, | ||
{ is_read: true }, | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}, | ||
); | ||
|
||
return { | ||
data: response.data, | ||
}; | ||
} catch (error) { | ||
return axios.isAxiosError(error) && error.response | ||
? { | ||
error: | ||
error.response.data.message || | ||
"Failed to mark notifications as read.", | ||
status: error.response.status, | ||
} | ||
: { | ||
error: "An unexpected error occurred.", | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
"use server"; | ||
|
||
import axios from "axios"; | ||
import { z } from "zod"; | ||
|
||
import { auth } from "~/lib/auth"; | ||
import { roleSchema } from "~/schemas"; | ||
|
||
const apiUrl = process.env.API_URL; | ||
|
||
export const getRoles = async (org_id: string) => { | ||
const session = await auth(); | ||
|
||
try { | ||
const response = await axios.get( | ||
`${apiUrl}/api/v1/organisations/${org_id}/roles`, | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${session?.access_token}`, | ||
}, | ||
}, | ||
); | ||
|
||
return { | ||
data: response.data, | ||
}; | ||
} catch (error) { | ||
return axios.isAxiosError(error) && error.response | ||
? { | ||
error: error.response.data.message || "Failed.", | ||
status: error.response.status, | ||
} | ||
: { | ||
error: "An unexpected error occurred.", | ||
}; | ||
} | ||
}; | ||
export const getPermissions = async () => { | ||
const session = await auth(); | ||
|
||
try { | ||
const response = await axios.get( | ||
`${apiUrl}/api/v1/organisations/permissions`, | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${session?.access_token}`, | ||
}, | ||
}, | ||
); | ||
|
||
return { | ||
data: response.data.data, | ||
}; | ||
} catch (error) { | ||
return axios.isAxiosError(error) && error.response | ||
? { | ||
error: error.response.data.message || "Failed.", | ||
status: error.response.status, | ||
} | ||
: { | ||
error: "An unexpected error occurred.", | ||
}; | ||
} | ||
}; | ||
|
||
export const getRolePermissions = async ( | ||
currentOrgId: string, | ||
roleId: string, | ||
) => { | ||
const session = await auth(); | ||
|
||
try { | ||
const response = await axios.get( | ||
`${apiUrl}/api/v1/organisations/${currentOrgId}/roles/${roleId}`, | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${session?.access_token}`, | ||
}, | ||
}, | ||
); | ||
|
||
return { | ||
data: response.data.data, | ||
}; | ||
} catch (error) { | ||
return axios.isAxiosError(error) && error.response | ||
? { | ||
error: error.response.data.message || "Failed.", | ||
status: error.response.status, | ||
} | ||
: { | ||
error: "An unexpected error occurred.", | ||
}; | ||
} | ||
}; | ||
|
||
export const createRole = async ( | ||
values: z.infer<typeof roleSchema>, | ||
org_id: string, | ||
) => { | ||
const validatedFields = roleSchema.safeParse(values); | ||
if (!validatedFields.success) { | ||
return { | ||
error: "Create Role Failed. Please check your inputs.", | ||
}; | ||
} | ||
|
||
const session = await auth(); | ||
const payload = validatedFields.data; | ||
|
||
try { | ||
const response = await axios.post( | ||
`${apiUrl}/api/v1/organisations/${org_id}/roles`, | ||
payload, | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${session?.access_token}`, | ||
}, | ||
}, | ||
); | ||
|
||
return { | ||
data: response.data, | ||
}; | ||
} catch (error) { | ||
return axios.isAxiosError(error) && error.response | ||
? { | ||
error: error.response.data.message || "Role creation failed.", | ||
status: error.response.status, | ||
} | ||
: { | ||
error: "An unexpected error occurred.", | ||
}; | ||
} | ||
}; | ||
const roleSchemaWithId = roleSchema.extend({ | ||
id: z.string().uuid(), | ||
permissions: z.array(z.string().uuid()), | ||
}); | ||
export const updateRole = async ( | ||
values: z.infer<typeof roleSchemaWithId>, | ||
currentOrgId: string, | ||
roleId: string, | ||
) => { | ||
const validatedFields = roleSchemaWithId.safeParse(values); | ||
if (!validatedFields.success) { | ||
return { | ||
error: "Create Role Failed. Please check your inputs.", | ||
}; | ||
} | ||
|
||
const session = await auth(); | ||
const payload = validatedFields.data; | ||
|
||
try { | ||
const response = await axios.put( | ||
`${apiUrl}/organisations/${currentOrgId}/roles/${roleId}`, | ||
payload, | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${session?.access_token}`, | ||
}, | ||
}, | ||
); | ||
return { | ||
data: response.data, | ||
}; | ||
} catch (error) { | ||
return axios.isAxiosError(error) && error.response | ||
? { | ||
error: error.response.data.message || "Role creation failed.", | ||
status: error.response.status, | ||
} | ||
: { | ||
error: "An unexpected error occurred.", | ||
}; | ||
} | ||
}; |
Oops, something went wrong.