-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[finishes #187354254] permissions and role implementation
finishes role and permission assigning functionality modified: package.json new file: src/controllers/permissionController.ts modified: src/controllers/roleControllers.ts new file: src/database/migrations/20240401004514-create-permissions-table.js modified: src/database/migrations/20240413203456-create-role.js renamed: src/database/migrations/20240425195548-create-user.js -> src/database/migrations/20240435195548-create-user.js new file: src/database/migrations/20240505075748-role_permissions.js new file: src/database/models/permission.ts modified: src/database/models/role.ts new file: src/database/models/rolePermissions.ts modified: src/database/models/user.ts modified: src/database/seeders/20240427082911-create-default-role.js new file: src/database/seeders/20240501105101-permissions.js new file: src/database/seeders/20240502080814-create-default-users.js new file: src/database/seeders/20240502093711-role_permissions.js new file: src/docs/permissions.yaml modified: src/middlewares/authMiddlewares.ts modified: src/routes/index.ts new file: src/routes/permissionRoute.ts modified: src/routes/roleRoute.ts new file: src/test/permissionController.test.ts
- Loading branch information
Showing
29 changed files
with
1,185 additions
and
121 deletions.
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
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,83 @@ | ||
import { Request, Response } from 'express'; | ||
import { sendInternalErrorResponse } from '../validations'; | ||
import Permission from '../database/models/permission'; | ||
import { validateFields } from '../validations/index'; | ||
|
||
// create a permission | ||
export const createPermission = async (req: Request, res: Response): Promise<void> => { | ||
try { | ||
const missingFields = validateFields(req, ['name']); | ||
if (missingFields.length) { | ||
res.status(400).json({ ok: false, errorMessage: `Missing required fields: ${missingFields.join(', ')}` }); | ||
return; | ||
} | ||
const createdPermission = await Permission.create({ name: req.body.name }); | ||
res.status(201).json({ ok: true, data: createdPermission }); | ||
} catch (error) { | ||
sendInternalErrorResponse(res, error); | ||
return; | ||
} | ||
}; | ||
// get all permissions | ||
export const getAllPermissions = async (req: Request, res: Response): Promise<void> => { | ||
try { | ||
const permissions = await Permission.findAll({ attributes: ['id', 'name'] }); | ||
res.status(200).json({ ok: true, data: permissions }); | ||
} catch (error) { | ||
sendInternalErrorResponse(res, error); | ||
return; | ||
} | ||
}; | ||
// get a single permission | ||
export const getSinglePermission = async (req: Request, res: Response): Promise<void> => { | ||
try { | ||
const permission = await Permission.findByPk(req.params.id); | ||
if (!permission) { | ||
res.status(404).json({ ok: false, errorMessage: 'Permission not found' }); | ||
return; | ||
} | ||
res.status(200).json({ ok: true, data: permission }); | ||
} catch (error) { | ||
sendInternalErrorResponse(res, error); | ||
return; | ||
} | ||
}; | ||
// update a permission | ||
export const updatePermission = async (req: Request, res: Response): Promise<void> => { | ||
try { | ||
const missingFields = validateFields(req, ['name']); | ||
if (missingFields.length) { | ||
res.status(400).json({ ok: false, errorMessage: `Missing required fields: ${missingFields.join(', ')}` }); | ||
return; | ||
} | ||
const permissionToUpdate = await Permission.findByPk(req.params.id); | ||
if (!permissionToUpdate) { | ||
res.status(404).json({ ok: false, errorMessage: 'Permission not found' }); | ||
return; | ||
} | ||
if (!req.body.name) res.status(400).json({ ok: false, errorMessage: 'Name is required' }); | ||
|
||
permissionToUpdate.name = req.body.name; | ||
|
||
await permissionToUpdate.save(); | ||
res.status(200).json({ ok: true, data: permissionToUpdate }); | ||
} catch (error) { | ||
sendInternalErrorResponse(res, error); | ||
return; | ||
} | ||
}; | ||
// delete a permission | ||
export const deletePermission = async (req: Request, res: Response): Promise<void> => { | ||
try { | ||
const permissionToDelete = await Permission.findByPk(req.params.id); | ||
if (!permissionToDelete) { | ||
res.status(404).json({ ok: false, errorMessage: 'Permission not found' }); | ||
return; | ||
} | ||
await permissionToDelete.destroy(); | ||
res.status(200).json({ ok: true, message: 'permission deleted successfully!' }); | ||
} catch (error) { | ||
sendInternalErrorResponse(res, error); | ||
return; | ||
} | ||
}; |
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,123 @@ | ||
import { Request, Response } from 'express'; | ||
import { sendInternalErrorResponse } from '../validations'; | ||
import logger from '../logs/config'; | ||
import VendorRequest from '../database/models/vendorRequest'; | ||
import uploadImage from '../helpers/claudinary'; | ||
import { validateFields } from '../validations'; | ||
import User from '../database/models/user'; | ||
const fileUploadService = async (req: Request) => { | ||
const store: string[] = []; | ||
for (const file of req.files as Express.Multer.File[]) { | ||
store.push(await uploadImage(file.buffer)); | ||
} | ||
return store; | ||
}; | ||
export const createVendorRequest = async (req: Request, res: Response): Promise<void> => { | ||
try { | ||
const vendorId = (await (req.user as any)).id; | ||
const exisistingUser = await VendorRequest.findOne({ where: { vendorId } }); | ||
if (exisistingUser) { | ||
logger.error('user request exists!'); | ||
res.status(400).json({ ok: false, message: 'this user has sent the request already!' }); | ||
return; | ||
} | ||
if (!req.files || +req.files.length < 6) { | ||
res.status(400).json({ message: 'Please upload all required documents(6)' }); | ||
return; | ||
} | ||
if (req.body.agreement !== 'true') { | ||
res.status(400).json({ message: 'Please agree to the terms and conditions' }); | ||
return; | ||
} | ||
const documents = await fileUploadService(req); | ||
|
||
logger.info('documents uploaded successfully!'); | ||
const { agreement } = req.body; | ||
|
||
const result = await VendorRequest.create({ | ||
vendorId, | ||
agreement, | ||
documents, | ||
}); | ||
res.status(201).json({ ok: true, data: result }); | ||
} catch (error) { | ||
logger.error(error); | ||
sendInternalErrorResponse(res, error); | ||
} | ||
}; | ||
export const getVendorRequest = async (req: Request, res: Response): Promise<void> => { | ||
try { | ||
const request = await VendorRequest.findByPk(req.params.id); | ||
if (!request) { | ||
res.status(404).json({ ok: false, message: `No request found by id: ${req.params.id}` }); | ||
} | ||
res.status(200).json({ ok: true, data: request }); | ||
} catch (error) { | ||
logger.error(error); | ||
sendInternalErrorResponse(res, error); | ||
} | ||
}; | ||
export const updateVendorRequest = async (req: Request, res: Response): Promise<void> => { | ||
try { | ||
if (req.body.agreement !== 'true') { | ||
res.status(400).json({ | ||
ok: false, | ||
errorMessage: `Please agree to our terms and conditions first`, | ||
}); | ||
return; | ||
} | ||
const request = await VendorRequest.findByPk(req.params.id); | ||
if (request === null) { | ||
res.status(404).json({ | ||
ok: false, | ||
message: 'request not found', | ||
}); | ||
} | ||
if (!req.files || +req.files.length !== 6) { | ||
res.status(400).json({ ok: false, message: '6 files are required' }); | ||
return; | ||
} | ||
const docs = await fileUploadService(req); | ||
request!.documents = docs; | ||
req.body.agreement === 'true' | ||
? (request!.agreement = req.body.agreement) | ||
: (request!.agreement = request!.agreement); | ||
await request?.save(); | ||
|
||
res.status(200).json({ ok: true, message: 'request updated successfully', data: request }); | ||
} catch (error) { | ||
logger.error(error); | ||
sendInternalErrorResponse(res, error); | ||
} | ||
}; | ||
export const deleteVendorRequest = async (req: Request, res: Response): Promise<void> => { | ||
try { | ||
const request = await VendorRequest.findByPk(req.params.id); | ||
if (request === null) { | ||
res.status(404).json({ | ||
ok: false, | ||
message: 'request not found', | ||
}); | ||
} | ||
await request?.destroy(); | ||
|
||
res.status(200).json({ ok: true, message: 'Vendor request deleted successfully' }); | ||
} catch (error) { | ||
logger.error(error); | ||
sendInternalErrorResponse(res, error); | ||
} | ||
}; | ||
export const getAllVendorRequests = async (req: Request, res: Response): Promise<void> => { | ||
try { | ||
const requests = await VendorRequest.findAll({ | ||
include: { | ||
model: User, | ||
}, | ||
}); | ||
|
||
res.status(200).json({ ok: true, data: requests }); | ||
} catch (error) { | ||
logger.error(error); | ||
sendInternalErrorResponse(res, error); | ||
} | ||
}; |
Oops, something went wrong.