82 lines
2.2 KiB
TypeScript
82 lines
2.2 KiB
TypeScript
import {
|
|
APIGatewayProxyEvent,
|
|
APIGatewayProxyResult,
|
|
Context,
|
|
} from 'aws-lambda';
|
|
|
|
import { prismaClient } from '../../../../common/database/prisma.lambda.service';
|
|
import { verifyHostToken } from '../../../../common/middlewares/jwt/authForHost';
|
|
import { safeHandler } from '../../../../common/utils/handlers/safeHandler';
|
|
import ApiError from '../../../../common/utils/helper/ApiError';
|
|
import { HostRolePermissionService } from '../../services/hostRolePermission.service';
|
|
|
|
const hostRolePermissionService = new HostRolePermissionService(prismaClient);
|
|
|
|
interface SaveRolePermissionsBody {
|
|
roleXid: number;
|
|
permissionMasterXids: number[];
|
|
}
|
|
|
|
export const handler = safeHandler(async (
|
|
event: APIGatewayProxyEvent,
|
|
context?: Context,
|
|
): Promise<APIGatewayProxyResult> => {
|
|
const token = event.headers['x-auth-token'] || event.headers['X-Auth-Token'];
|
|
|
|
if (!token) {
|
|
throw new ApiError(
|
|
400,
|
|
'This is a protected route. Please provide a valid token.',
|
|
);
|
|
}
|
|
|
|
const userInfo = await verifyHostToken(token);
|
|
|
|
let body: Partial<SaveRolePermissionsBody> = {};
|
|
if (event.body) {
|
|
try {
|
|
body = JSON.parse(event.body);
|
|
} catch {
|
|
throw new ApiError(400, 'Invalid JSON body');
|
|
}
|
|
}
|
|
|
|
const roleXid = Number(body.roleXid);
|
|
const permissionMasterXids = Array.isArray(body.permissionMasterXids)
|
|
? body.permissionMasterXids
|
|
: [];
|
|
|
|
if (!Number.isInteger(roleXid) || roleXid <= 0) {
|
|
throw new ApiError(400, 'roleXid is required.');
|
|
}
|
|
|
|
if (!permissionMasterXids.length) {
|
|
throw new ApiError(400, 'permissionMasterXids is required.');
|
|
}
|
|
|
|
const result = await hostRolePermissionService.saveRolePermissions({
|
|
hostUserXid: userInfo.id,
|
|
roleXid,
|
|
permissionMasterXids,
|
|
});
|
|
|
|
return {
|
|
statusCode: 200,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Access-Control-Allow-Origin': '*',
|
|
},
|
|
body: JSON.stringify({
|
|
success: true,
|
|
message: 'Role permissions saved successfully',
|
|
data: {
|
|
permissionMasterXid: result.saved.id,
|
|
hostXid: result.saved.hostXid,
|
|
roleXid: result.saved.roleXid,
|
|
permissionMasterXids: result.saved.permissionMasterXids,
|
|
selectedPermissions: result.selectedPermissions,
|
|
},
|
|
}),
|
|
};
|
|
});
|