-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new corporate management feature
- Loading branch information
Showing
5 changed files
with
185 additions
and
0 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,77 @@ | ||
const Corporation = require('../models/corporationModel'); | ||
|
||
// List Corporations with Pagination, Filtering, and Access Control | ||
exports.listCorporations = async (req, res) => { | ||
try { | ||
const { page = 1, limit = 10, search = '' } = req.query; | ||
|
||
const query = search ? { legalName: new RegExp(search, 'i') } : {}; | ||
const corporations = await Corporation.find(query) | ||
.limit(limit * 1) | ||
.skip((page - 1) * limit) | ||
.exec(); | ||
|
||
const count = await Corporation.countDocuments(query); | ||
|
||
res.json({ | ||
corporations, | ||
totalPages: Math.ceil(count / limit), | ||
currentPage: page, | ||
}); | ||
} catch (error) { | ||
res.status(500).json({ message: error.message }); | ||
} | ||
}; | ||
|
||
// Create New Corporation | ||
exports.createCorporation = async (req, res) => { | ||
try { | ||
const { id, legalName, doingBusinessAsName, website } = req.body; | ||
|
||
if (!id || !legalName) { | ||
return res.status(400).json({ message: 'ID and Legal Name are required' }); | ||
} | ||
|
||
const newCorporation = new Corporation({ id, legalName, doingBusinessAsName, website }); | ||
await newCorporation.save(); | ||
|
||
res.status(201).json(newCorporation); | ||
} catch (error) { | ||
res.status(500).json({ message: error.message }); | ||
} | ||
}; | ||
|
||
// Update Corporation Details | ||
exports.updateCorporation = async (req, res) => { | ||
try { | ||
const { id } = req.params; | ||
const updateData = req.body; | ||
|
||
const corporation = await Corporation.findByIdAndUpdate(id, updateData, { new: true }); | ||
|
||
if (!corporation) { | ||
return res.status(404).json({ message: 'Corporation not found' }); | ||
} | ||
|
||
res.json(corporation); | ||
} catch (error) { | ||
res.status(500).json({ message: error.message }); | ||
} | ||
}; | ||
|
||
// Delete Corporation | ||
exports.deleteCorporation = async (req, res) => { | ||
try { | ||
const { id } = req.params; | ||
|
||
const corporation = await Corporation.findByIdAndDelete(id); | ||
|
||
if (!corporation) { | ||
return res.status(404).json({ message: 'Corporation not found' }); | ||
} | ||
|
||
res.json({ message: 'Corporation deleted successfully' }); | ||
} catch (error) { | ||
res.status(500).json({ message: error.message }); | ||
} | ||
}; |
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,15 @@ | ||
const mongoose = require('mongoose'); | ||
const { Schema } = mongoose; | ||
|
||
const CorporationSchema = new Schema({ | ||
id: { type: String, required: true, unique: true }, | ||
legalName: { type: String, required: true }, | ||
doingBusinessAsName: { type: String }, | ||
website: { type: String } | ||
}, { | ||
timestamps: true | ||
}); | ||
|
||
const Corporation = mongoose.model('Corporation', CorporationSchema); | ||
|
||
module.exports = Corporation; |
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,17 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
const corporationController = require('../controllers/corporationController'); | ||
|
||
// List Corporations | ||
router.get('/corporations', corporationController.listCorporations); | ||
|
||
// Create New Corporation | ||
router.post('/corporations', corporationController.createCorporation); | ||
|
||
// Update Corporation Details | ||
router.put('/corporations/:id', corporationController.updateCorporation); | ||
|
||
// Delete Corporation | ||
router.delete('/corporations/:id', corporationController.deleteCorporation); | ||
|
||
module.exports = 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
const request = require('supertest'); | ||
const mongoose = require('mongoose'); | ||
const { MongoMemoryServer } = require('mongodb-memory-server'); | ||
const app = require('../app'); | ||
const Corporation = require('../models/corporationModel'); | ||
|
||
let mongoServer; | ||
|
||
beforeAll(async () => { | ||
mongoServer = await MongoMemoryServer.create(); | ||
const uri = mongoServer.getUri(); | ||
await mongoose.connect(uri, { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true, | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
await mongoose.disconnect(); | ||
await mongoServer.stop(); | ||
}); | ||
|
||
describe('Corporation Management', () => { | ||
|
||
beforeEach(async () => { | ||
// Reset the database before each test | ||
await Corporation.deleteMany({}); | ||
}); | ||
|
||
it('should list corporations with pagination', async () => { | ||
await Corporation.create({ id: '1', legalName: 'Corp One' }); | ||
await Corporation.create({ id: '2', legalName: 'Corp Two' }); | ||
|
||
const res = await request(app).get('/api/v2/corporations?page=1&limit=10'); | ||
|
||
expect(res.statusCode).toEqual(200); | ||
expect(res.body.corporations.length).toEqual(2); | ||
expect(res.body.corporations[0].legalName).toEqual('Corp One'); | ||
}); | ||
|
||
it('should create a new corporation', async () => { | ||
const res = await request(app) | ||
.post('/api/v2/corporations') | ||
.send({ id: '1', legalName: 'Corp One', website: 'http://corpone.com' }); | ||
|
||
expect(res.statusCode).toEqual(201); | ||
expect(res.body.legalName).toEqual('Corp One'); | ||
}); | ||
|
||
it('should update a corporation\'s details', async () => { | ||
const corp = await Corporation.create({ id: '1', legalName: 'Corp One' }); | ||
|
||
const res = await request(app) | ||
.put(`/api/v2/corporations/${corp._id}`) | ||
.send({ legalName: 'Corp One Updated' }); | ||
|
||
expect(res.statusCode).toEqual(200); | ||
expect(res.body.legalName).toEqual('Corp One Updated'); | ||
}); | ||
|
||
it('should delete a corporation', async () => { | ||
const corp = await Corporation.create({ id: '1', legalName: 'Corp One' }); | ||
|
||
const res = await request(app) | ||
.delete(`/api/v2/corporations/${corp._id}`); | ||
|
||
expect(res.statusCode).toEqual(200); | ||
expect(res.body.message).toEqual('Corporation deleted successfully'); | ||
|
||
const findRes = await Corporation.findById(corp._id); | ||
expect(findRes).toBeNull(); | ||
}); | ||
|
||
}); |