Skip to content

Commit

Permalink
Merge branch 'dev' into feat/superadmin-user-management
Browse files Browse the repository at this point in the history
  • Loading branch information
incredible-phoenix246 authored Aug 16, 2024
2 parents a721f0d + 7cc67af commit a6e6ba1
Show file tree
Hide file tree
Showing 21 changed files with 1,370 additions and 235 deletions.
Binary file added public/images/bell-icon.jpg
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.
32 changes: 32 additions & 0 deletions src/actions/notifications/deleteAllNotifications.ts
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.",
};
}
};
40 changes: 40 additions & 0 deletions src/actions/notifications/deleteNotifications.ts
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.",
};
}
};
33 changes: 33 additions & 0 deletions src/actions/notifications/getAllNotifications.ts
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.",
};
}
};
34 changes: 34 additions & 0 deletions src/actions/notifications/markAllAsRead.ts
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.",
};
}
};
42 changes: 42 additions & 0 deletions src/actions/notifications/markOneAsRead.ts
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.",
};
}
};
178 changes: 178 additions & 0 deletions src/actions/roles-and-permissions.ts
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.",
};
}
};
Loading

0 comments on commit a6e6ba1

Please sign in to comment.