Skip to content

Commit

Permalink
feat(management/role): add Assignable role option (#1026)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusualhashash authored Dec 17, 2024
2 parents 64a1819 + 5d20f0e commit ae3d3b9
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 135 deletions.
2 changes: 1 addition & 1 deletion apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"dependencies": {
"@auth/core": "^0.30.0",
"@ayasofyazilim/saas": "0.0.87",
"@ayasofyazilim/saas": "0.0.90",
"@formatjs/intl-localematcher": "^0.5.4",
"@radix-ui/react-icons": "^1.3.0",
"@repo/ayasofyazilim-ui": "workspace:^",
Expand Down
19 changes: 18 additions & 1 deletion apps/web/src/actions/core/IdentityService/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import type {
PutApiIdentityRolesByIdMoveAllUsersData,
PutApiIdentityUsersByIdClaimsData,
} from "@ayasofyazilim/saas/IdentityService";
import { structuredError, structuredResponse } from "src/lib";
import {
getIdentityServiceClient,
structuredError,
structuredResponse,
} from "src/lib";
import { getApiRequests } from "../../api-requests";

export async function getClaimsApi(body: GetApiIdentityClaimTypesData = {}) {
Expand Down Expand Up @@ -143,3 +147,16 @@ export async function deleteUserSessionsApi(id: string) {
return structuredError(error);
}
}

export async function getAssignableRolesApi(id: string) {
try {
const client = await getIdentityServiceClient();
const dataResponse =
await client.assignableRole.getApiIdentityAssignableRolesAllRolesWithAssignableById(
{ id },
);
return structuredResponse(dataResponse);
} catch (error) {
return structuredError(error);
}
}
20 changes: 19 additions & 1 deletion apps/web/src/actions/core/IdentityService/put-actions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
"use server";
import type {
PutApiIdentityAssignableRolesData,
PutApiIdentityUsersByIdChangePasswordData,
PutApiIdentityUsersByIdTwoFactorByEnabledData,
PutApiOpeniddictApplicationsByIdTokenLifetimeData,
} from "@ayasofyazilim/saas/IdentityService";
import { structuredError, structuredResponse } from "src/lib";
import {
getIdentityServiceClient,
structuredError,
structuredResponse,
} from "src/lib";
import { getApiRequests } from "../../api-requests";

export async function putApplicationTokenLifetimeApi(
Expand Down Expand Up @@ -42,3 +47,16 @@ export async function putUserTwoFactorApi(
return structuredError(error);
}
}

export async function putAssignableRolesApi(
data: PutApiIdentityAssignableRolesData,
) {
try {
const client = await getIdentityServiceClient();
const dataResponse =
await client.assignableRole.putApiIdentityAssignableRoles(data);
return structuredResponse(dataResponse);
} catch (error) {
return structuredError(error);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { dataConfig } from "../../data.tsx";
import Claims from "./table-actions/claims.tsx";
import PermissionsComponent from "./table-actions/permissions.tsx";
import SessionsComponent from "./table-actions/sessions.tsx";
import AssignableRoles from "./table-actions/assignable-roles.tsx";

async function controlledFetch(
url: string,
Expand Down Expand Up @@ -538,7 +539,30 @@ export default function Page({
content: <></>,
});
}

const assingableRoles = async (
row: { id: string },
setIsOpen?: (e: boolean) => void,
) => {
await Promise.resolve();
return (
<AssignableRoles
lang={params.lang}
rowId={row.id}
setIsOpen={setIsOpen}
/>
);
};
if (params.data === "role") {
columnsData.data.actionList?.push({
type: "Dialog",
cta: languageData["Role.Assignable"],
loadingContent: <>Loading...</>,
description: languageData["Role.Assignable.Description"],
componentType: "CustomComponent",
customComponentRendering: (setIsOpen) => assingableRoles(setIsOpen),
content: <></>,
});
}
return (
<Dashboard
action={action}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
"use client";
import { toast } from "@/components/ui/sonner";
import type {
UniRefund_IdentityService_AssignableRoles_AssignableRoleDto,
UniRefund_IdentityService_AssignableRoles_UpsertAssignableRoleDto,
} from "@ayasofyazilim/saas/IdentityService";
import { $UniRefund_IdentityService_AssignableRoles_UpsertAssignableRoleDto } from "@ayasofyazilim/saas/IdentityService";
import { createZodObject } from "@repo/ayasofyazilim-ui/lib/create-zod-object";
import { MultiSelect } from "@repo/ayasofyazilim-ui/molecules/multi-select";
import AutoForm, {
AutoFormSubmit,
} from "@repo/ayasofyazilim-ui/organisms/auto-form";
import { useEffect, useState } from "react";
import { getAssignableRolesApi } from "src/actions/core/IdentityService/actions";
import { putAssignableRolesApi } from "src/actions/core/IdentityService/put-actions";
import { getResourceDataClient } from "src/language-data/core/IdentityService";

const assignableRoleSchema = createZodObject(
$UniRefund_IdentityService_AssignableRoles_UpsertAssignableRoleDto,
["targetRoleIds"],
);
export default function AssignableRoles({
rowId,
lang,
setIsOpen,
}: {
rowId: string;
lang: string;
setIsOpen?: (e: boolean) => void;
}) {
const languageData = getResourceDataClient(lang);

const [assignableRoles, setAssignableRoles] = useState<
UniRefund_IdentityService_AssignableRoles_AssignableRoleDto[]
>([]);

useEffect(() => {
const fetchAssignableRoles = async () => {
const response = await getAssignableRolesApi(rowId);
if (response.type === "success") {
setAssignableRoles(response.data);
} else {
toast.error(
`${response.status}: ${
response.message || languageData["Role.AssignableRoles.Get.Fail"]
}`,
);
}
};
void fetchAssignableRoles();
}, []);

const putAssignableRoles = async (
formData: UniRefund_IdentityService_AssignableRoles_UpsertAssignableRoleDto,
) => {
const response = await putAssignableRolesApi({
requestBody: {
sourceRoleId: rowId,
targetRoleIds: formData.targetRoleIds,
},
});
if (response.type === "success") {
toast.success(languageData["Role.AssignableRoles.Put.Success"]);
setIsOpen && setIsOpen(false);
} else {
toast.error(
`${response.status}: ${
response.message || languageData["Role.AssignableRoles.Put.Fail"]
}`,
);
}
};

return (
<AutoForm
fieldConfig={{
targetRoleIds: {
renderer: function RoleComboboxRenderer(props) {
return (
<div className="mb-1 w-full">
<label className="text-bold mb-1 block text-sm">
{languageData["Role.Names"]}
</label>
<MultiSelect
defaultValue={assignableRoles
.filter((role) => role.isAssignable)
.map((role) => role.roleId)}
onValueChange={(e) => {
props.field.onChange(e);
}}
options={assignableRoles.map((role) => ({
label: role.roleName || "",
value: role.roleId || "",
}))}
placeholder={languageData["Role.Select"]}
/>
</div>
);
},
},
}}
formSchema={assignableRoleSchema}
onSubmit={(data) => {
void putAssignableRoles(
data as UniRefund_IdentityService_AssignableRoles_UpsertAssignableRoleDto,
);
}}
>
<AutoFormSubmit className="float-right">
{languageData["Edit.Save"]}
</AutoFormSubmit>
</AutoForm>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"Role.MoveAllUsers.Update.Success": "All users moved successfully",
"Role.Select": "Select role",
"Role.Unassigned": "Unassign role",
"Role.Assignable": "Assignable roles",
"Role.Assignable.Description": "You can update the assignable roles from here.",
"Role.AssignableRoles.Get.Fail": "Failed to fetch assignable roles",
"Role.AssignableRoles.Put.Success": "Assignable roles updated successfully",
"Role.AssignableRoles.Put.Fail": "Failed to update assignable roles",
"Form.isDefault": "Default",
"Form.isPublic": "Public",

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"Role.MoveAllUsers.Update.Success": "Tüm kullanıcılar başarıyla taşındı.",
"Role.Select": "Rol seç",
"Role.Unassigned": "Rolü Kaldır",
"Role.Assignable": "Atanabilir roller",
"Role.Assignable.Description": "Atanabilir rolleri buradan güncellenebilirsiniz.",
"Role.AssignableRoles.Get.Fail": "Atanabilir roller alınırken hata oluştu",
"Role.AssignableRoles.Put.Success": "Atanabilir roller başarıyla güncellendi",
"Role.AssignableRoles.Put.Fail": "Atanabilir roller güncellenirken hata oluştu",
"Form.isDefault": "Varsayılan",
"Form.isPublic": "Herkese Açık",

Expand Down
Loading

0 comments on commit ae3d3b9

Please sign in to comment.