-
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
24 changed files
with
1,126 additions
and
92 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
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 validFields = validateFields(req, ['name']); | ||
if (validFields.length) { | ||
res.status(400).json({ ok: false, errorMessage: `Missing required fields: ${validFields.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 validFields = validateFields(req, ['name']); | ||
if (validFields.length) { | ||
res.status(400).json({ ok: false, errorMessage: `Missing required fields: ${validFields.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
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'; | ||
|
||
export const createVendorRequest = async (req: Request, res: Response): Promise<void> => { | ||
try { | ||
if (!req.files || +req.files.length < 6) { | ||
res.status(400).json({ message: 'Please upload all required documents(6)' }); | ||
return; | ||
} | ||
|
||
const missingFields = validateFields(req, ['vendorId', 'agreement']); | ||
if (missingFields.length > 0) { | ||
res.status(400).json({ message: `Please fill in all required fields: ${missingFields.join(', ')}` }); | ||
return; | ||
} | ||
if (!req.body.agreement) { | ||
res.status(400).json({ message: 'Please agree to the terms and conditions' }); | ||
return; | ||
} | ||
const documents = []; | ||
for (const file of req.files as Express.Multer.File[]) { | ||
documents.push(await uploadImage(file.buffer)); | ||
} | ||
const { vendorId, agreement, status } = req.body; | ||
const result = await VendorRequest.create({ vendorId, agreement, documents, status }); | ||
console.log(result); | ||
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 { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
|
||
res.status(200).json({ message: 'Vendor request retrieved successfully' }); | ||
} catch (error) { | ||
logger.error(error); | ||
sendInternalErrorResponse(res, error); | ||
} | ||
}; | ||
export const updateVendorRequest = async (req: Request, res: Response): Promise<void> => { | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
|
||
res.status(200).json({ message: 'Vendor request updated successfully' }); | ||
} catch (error) { | ||
logger.error(error); | ||
sendInternalErrorResponse(res, error); | ||
} | ||
}; | ||
export const deleteVendorRequest = async (req: Request, res: Response): Promise<void> => { | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
|
||
res.status(200).json({ message: 'Vendor request deleted successfully' }); | ||
} catch (error) { | ||
logger.error(error); | ||
sendInternalErrorResponse(res, error); | ||
} | ||
}; | ||
export const getAllVendorRequests = async (req: Request, res: Response): Promise<void> => { | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
|
||
res.status(200).json({ message: 'All vendor requests retrieved successfully' }); | ||
} catch (error) { | ||
logger.error(error); | ||
sendInternalErrorResponse(res, error); | ||
} | ||
}; |
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
32 changes: 32 additions & 0 deletions
32
src/database/migrations/20240501104514-create-permissions-table.js
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,32 @@ | ||
'use strict'; | ||
|
||
/** @type {import('sequelize-cli').Migration} */ | ||
module.exports = { | ||
async up(queryInterface, Sequelize) { | ||
await queryInterface.createTable('Permissions', { | ||
id: { | ||
type: Sequelize.INTEGER, | ||
primaryKey: true, | ||
autoIncrement: true, | ||
}, | ||
name: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
defaultValue: 'read', | ||
unique: true, | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
}, | ||
}); | ||
}, | ||
|
||
async down(queryInterface, Sequelize) { | ||
await queryInterface.dropTable('Permissions'); | ||
}, | ||
}; |
Oops, something went wrong.