-
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
inishes 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 Changes to be committed: new file: src/controllers/permissionController.ts modified: src/controllers/roleControllers.ts modified: src/controllers/userController.ts new file: src/controllers/vendorRequestController.ts modified: src/database/migrations/20240413203456-create-role.js modified: src/database/migrations/20240425195548-create-user.js new file: src/database/migrations/20240501104514-create-permissions-table.js new file: src/database/migrations/20240505075748-role_permissions.js new file: src/database/migrations/20240506142003-create_vendor_request_table.js modified: src/database/models/index.ts new file: src/database/models/order.ts new file: src/database/models/orderItem.ts new file: src/database/models/vendorRequest.ts modified: src/database/seeders/20240427082911-create-default-rol e.js deleted: src/database/seeders/20240429200629-add-seller-role.js new file: src/database/seeders/20240501105101-permissions.js modified: src/database/seeders/20240501163745-User.js new file: src/docs/permissions.yaml new file: src/docs/vendor.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/routes/vendorRequestRoute.ts modified: src/server.ts
- Loading branch information
Showing
30 changed files
with
1,184 additions
and
111 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { Request, Response } from 'express'; | ||
import { sendInternalErrorResponse } from '../validations'; | ||
import logger from '../logs/config'; | ||
import VendorRequest from '../database/models/sellerRequest'; | ||
import uploadImage from '../helpers/claudinary'; | ||
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 createSellerRequest = async (req: Request, res: Response): Promise<void> => { | ||
try { | ||
const vendorId = (req.user as User).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 getSellerRequest = 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 updateSellerRequest = 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 deleteSellerRequest = 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 getAllSellerRequests = 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); | ||
} | ||
}; |
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
Oops, something went wrong.