-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
User Management Navigator Supervisor Backend Implementation #120
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,46 @@ | ||
import { userService, accessRequestService } from '../services'; | ||
|
||
import type { NextFunction, Request, Response } from 'express'; | ||
|
||
import type { UserAccessRequest } from '../types'; | ||
|
||
const controller = { | ||
// Request to create user & access | ||
createUserAccessRevokeRequest: async (req: Request, res: Response, next: NextFunction) => { | ||
// TODO check if the calling user is a supervisor or an admin | ||
try { | ||
let response; | ||
const { user, accessRequest } = req.body; | ||
if (accessRequest?.grant === false) { | ||
response = await accessRequestService.createUserAccessRevokeRequest(accessRequest); | ||
res.status(201).json(response); | ||
} else { | ||
const userResponse = await userService.createUserIfNew(user); | ||
if (userResponse) { | ||
accessRequest.userId = userResponse.userId; | ||
response = userResponse as UserAccessRequest; | ||
response.accessRequest = await accessRequestService.createUserAccessRevokeRequest(accessRequest); | ||
res.status(201).json(response); | ||
} else { | ||
// TODO check if the user is a proponent | ||
// Put an entry in accessRequest table | ||
// Send 409 if the user is not a proponent | ||
res.status(409).json({ message: 'User already exists' }); | ||
} | ||
} | ||
} catch (e: unknown) { | ||
next(e); | ||
} | ||
}, | ||
|
||
getAccessRequests: async (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
const response = await accessRequestService.getAccessRequests(); | ||
res.status(200).json(response); | ||
} catch (e: unknown) { | ||
next(e); | ||
} | ||
} | ||
}; | ||
|
||
export default controller; |
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
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
51 changes: 51 additions & 0 deletions
51
app/src/db/migrations/20240717000000_007_access-request.ts
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,51 @@ | ||
import stamps from '../stamps'; | ||
|
||
import type { Knex } from 'knex'; | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
return ( | ||
Promise.resolve() | ||
// Create the access_request table | ||
.then(() => | ||
knex.schema.createTable('access_request', (table) => { | ||
table.uuid('access_request_id').primary(); | ||
table.uuid('user_id').notNullable().references('user_id').inTable('user'); | ||
table.text('role'); | ||
table | ||
.enu('status', ['Approved', 'Pending', 'Rejected'], { | ||
useNative: true, | ||
enumName: 'access_request_status_enum' | ||
}) | ||
.defaultTo('Pending') | ||
.notNullable(); | ||
table.boolean('grant').notNullable(); | ||
stamps(knex, table); | ||
}) | ||
) | ||
|
||
.then(() => | ||
knex.schema.raw(`create trigger before_update_access_request_trigger | ||
before update on public.access_request | ||
for each row execute procedure public.set_updated_at();`) | ||
) | ||
|
||
.then(() => | ||
knex.schema.raw(`CREATE TRIGGER audit_access_request_trigger | ||
AFTER UPDATE OR DELETE ON access_request | ||
FOR EACH ROW EXECUTE PROCEDURE audit.if_modified_func();`) | ||
) | ||
); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
return ( | ||
Promise.resolve() | ||
// Drop triggers | ||
.then(() => knex.schema.raw('DROP TRIGGER IF EXISTS before_update_access_request_trigger ON access_request')) | ||
.then(() => knex.schema.raw('DROP TRIGGER IF EXISTS audit_access_request_trigger ON access_request')) | ||
// Drop the access_request table | ||
.then(() => knex.schema.dropTableIfExists('access_request')) | ||
// Drop the access_request_status_enum type | ||
.then(() => knex.schema.raw('DROP TYPE IF EXISTS access_request_status_enum')) | ||
); | ||
} |
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 @@ | ||
import { Prisma } from '@prisma/client'; | ||
|
||
import { AccessRequestStatus } from '../../utils/enums/application'; | ||
|
||
import type { Stamps } from '../stamps'; | ||
import type { AccessRequest } from '../../types/AccessRequest'; // Import the access_request_status_enum type | ||
|
||
// Define types | ||
const _accessRequest = Prisma.validator<Prisma.access_requestDefaultArgs>()({}); | ||
|
||
type PrismaRelationAccessRequest = Omit<Prisma.access_requestGetPayload<typeof _accessRequest>, keyof Stamps>; | ||
|
||
export default { | ||
toPrismaModel(input: AccessRequest): PrismaRelationAccessRequest { | ||
return { | ||
access_request_id: input.accessRequestId, | ||
grant: input.grant, | ||
role: input.role, | ||
status: input.status as AccessRequestStatus, // Cast the status property to AccessRequestStatus enum | ||
user_id: input.userId | ||
}; | ||
}, | ||
|
||
fromPrismaModel(input: PrismaRelationAccessRequest): AccessRequest { | ||
return { | ||
accessRequestId: input.access_request_id, | ||
grant: input.grant, | ||
role: input.role, | ||
userId: input.user_id as string, | ||
status: input.status as AccessRequestStatus // Cast the status property to AccessRequestStatus enum | ||
}; | ||
} | ||
}; |
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
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
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,30 @@ | ||
import express from 'express'; | ||
import { accessRequestController } from '../../controllers'; | ||
import { requireSomeAuth } from '../../middleware/requireSomeAuth'; | ||
import { accessRequestValidator } from '../../validators'; | ||
|
||
import type { NextFunction, Request, Response } from 'express'; | ||
|
||
const router = express.Router(); | ||
router.use(requireSomeAuth); | ||
|
||
// Request to create/revoke a user and access request - called by supervisor(201) & admin(200) | ||
router.post( | ||
'/', | ||
accessRequestValidator.userAccessRevokeRequest, | ||
(req: Request, res: Response, next: NextFunction): void => { | ||
accessRequestController.createUserAccessRevokeRequest(req, res, next); | ||
} | ||
); | ||
|
||
// Approve/Deny access/revoke request - called by admin (200) | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
router.patch('/', (req: Request, res: Response, next: NextFunction): void => { | ||
// TODO: approve/deny access request | ||
}); | ||
|
||
router.get('/', (req: Request, res: Response, next: NextFunction): void => { | ||
accessRequestController.getAccessRequests(req, res, next); | ||
}); | ||
|
||
export default router; |
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
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
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,59 @@ | ||
// import jwt from 'jsonwebtoken'; | ||
// import { Prisma } from '@prisma/client'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
|
||
import prisma from '../db/dataConnection'; | ||
import { access_request } from '../db/models'; | ||
import { AccessRequestStatus } from '../utils/enums/application'; | ||
|
||
import type { AccessRequest } from '../types'; | ||
|
||
/** | ||
* The User DB Service | ||
*/ | ||
const service = { | ||
/** | ||
* @function createUserAccessRequest | ||
* Create an access_request record | ||
* @param {object} data Incoming accessRequest data | ||
* @returns {Promise<object>} The result of running the insert operation | ||
* @throws The error encountered upon db transaction failure | ||
*/ | ||
createUserAccessRevokeRequest: async (accessRequest: AccessRequest) => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
|
||
const newAccessRequest = { | ||
accessRequestId: uuidv4(), | ||
userId: accessRequest.userId, | ||
grant: accessRequest.grant as boolean, | ||
role: accessRequest.role as string, | ||
status: AccessRequestStatus.PENDING | ||
}; | ||
const accessRequestResponse = await prisma.access_request.create({ | ||
data: access_request.toPrismaModel(newAccessRequest) | ||
}); | ||
|
||
return access_request.fromPrismaModel(accessRequestResponse); | ||
}, | ||
|
||
/** | ||
* @function getAccessRequests | ||
* Get all access requests | ||
* @returns {Promise<object>} The result of running the find operation | ||
*/ | ||
getAccessRequests: async () => { | ||
const response = await prisma.access_request.findMany(); | ||
return response.map((x) => access_request.fromPrismaModel(x)); | ||
}, | ||
|
||
/** | ||
* @function updateUserRole | ||
* Updates user role | ||
* @returns {Promise<object>} The result of running the put operation | ||
*/ | ||
updateUserRole: async () => { | ||
// TODO: Implement updateUserRole | ||
} | ||
}; | ||
|
||
export default service; |
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
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
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,10 @@ | ||
import { IStamps } from '../interfaces/IStamps'; | ||
import { AccessRequestStatus } from '../utils/enums/application'; | ||
|
||
export type AccessRequest = { | ||
accessRequestId: string; // Primary key | ||
grant: boolean; | ||
role: string | null; | ||
status: AccessRequestStatus; | ||
userId: string; | ||
} & Partial<IStamps>; |
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,4 @@ | ||
import type { AccessRequest, User } from '.'; | ||
export type UserAccessRequest = { | ||
accessRequest?: AccessRequest; | ||
} & User; |
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 |
---|---|---|
|
@@ -8,4 +8,5 @@ export type UserSearchParameters = { | |
fullName?: string; | ||
lastName?: string; | ||
active?: boolean; | ||
role?: string; | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Transaction not required at this level.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done