-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #851 from idurar/feat/client-company-people-done
client backend and frontend done
Showing
48 changed files
with
643 additions
and
189 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
backend/src/controllers/appControllers/clientController/create.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,51 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const Model = mongoose.model('Client'); | ||
const People = mongoose.model('People'); | ||
const Company = mongoose.model('Company'); | ||
|
||
const create = async (req, res) => { | ||
// Creating a new document in the collection | ||
|
||
if (req.body.type === 'people') { | ||
if (!req.body.people) { | ||
return res.status(403).json({ | ||
success: false, | ||
message: 'Please select a person', | ||
}); | ||
} else { | ||
let { firstname, lastname } = await People.findOne({ | ||
_id: req.body.people, | ||
removed: false, | ||
}).exec(); | ||
req.body.name = firstname + ' ' + lastname; | ||
req.body.company = undefined; | ||
} | ||
} else { | ||
if (!req.body.company) { | ||
return res.status(403).json({ | ||
success: false, | ||
message: 'Please select a company', | ||
}); | ||
} else { | ||
let { name } = await Company.findOne({ | ||
_id: req.body.company, | ||
removed: false, | ||
}).exec(); | ||
req.body.name = name; | ||
req.body.people = undefined; | ||
} | ||
} | ||
|
||
req.body.removed = false; | ||
const result = await new Model(req.body).save(); | ||
|
||
// Returning successfull response | ||
return res.status(200).json({ | ||
success: true, | ||
result, | ||
message: 'Successfully Created the document in Model ', | ||
}); | ||
}; | ||
|
||
module.exports = create; |
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
29 changes: 29 additions & 0 deletions
29
backend/src/controllers/appControllers/clientController/listAll.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,29 @@ | ||
const { migrate } = require('./migrate'); | ||
|
||
const mongoose = require('mongoose'); | ||
|
||
const Model = mongoose.model('Client'); | ||
|
||
const listAll = async (req, res) => { | ||
const sort = parseInt(req.query.sort) || 'desc'; | ||
|
||
// Query the database for a list of all results | ||
const result = await Model.find({ removed: false }).sort({ created: sort }).populate().exec(); | ||
|
||
const migratedData = result.map((x) => migrate(x)); | ||
if (result.length > 0) { | ||
return res.status(200).json({ | ||
success: true, | ||
result: migratedData, | ||
message: 'Successfully found all documents', | ||
}); | ||
} else { | ||
return res.status(203).json({ | ||
success: false, | ||
result: [], | ||
message: 'Collection is Empty', | ||
}); | ||
} | ||
}; | ||
|
||
module.exports = listAll; |
15 changes: 15 additions & 0 deletions
15
backend/src/controllers/appControllers/clientController/migrate.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,15 @@ | ||
exports.migrate = (result) => { | ||
const client = result.type === 'people' ? result.people : result.company; | ||
let newData = {}; | ||
newData._id = result._id; | ||
newData.type = result.type; | ||
newData.name = result.name; | ||
newData.phone = client.phone; | ||
newData.email = client.email; | ||
newData.website = client.website; | ||
newData.country = client.country; | ||
newData.address = client.address; | ||
newData.people = result.people; | ||
newData.company = result.company; | ||
return newData; | ||
}; |
49 changes: 49 additions & 0 deletions
49
backend/src/controllers/appControllers/clientController/paginatedList.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,49 @@ | ||
const { migrate } = require('./migrate'); | ||
const mongoose = require('mongoose'); | ||
|
||
const Model = mongoose.model('Client'); | ||
|
||
const paginatedList = async (req, res) => { | ||
const page = req.query.page || 1; | ||
|
||
const limit = parseInt(req.query.items) || 10; | ||
const skip = page * limit - limit; | ||
|
||
// Query the database for a list of all results | ||
const resultsPromise = Model.find({ removed: false }) | ||
.skip(skip) | ||
.limit(limit) | ||
.sort({ created: 'desc' }) | ||
.populate() | ||
.exec(); | ||
|
||
// Counting the total documents | ||
const countPromise = Model.countDocuments({ removed: false }); | ||
// Resolving both promises | ||
const [result, count] = await Promise.all([resultsPromise, countPromise]); | ||
// console.log('🚀 ~ file: paginatedList.js:23 ~ paginatedList ~ result:', result); | ||
|
||
// Calculating total pages | ||
const pages = Math.ceil(count / limit); | ||
|
||
const pagination = { page, pages, count }; | ||
if (count > 0) { | ||
const migratedData = result.map((x) => migrate(x)); | ||
// console.log('🚀 ~ file: paginatedList.js:23 ~ paginatedList ~ migratedData:', migratedData); | ||
return res.status(200).json({ | ||
success: true, | ||
result: migratedData, | ||
pagination, | ||
message: 'Successfully found all documents', | ||
}); | ||
} else { | ||
return res.status(203).json({ | ||
success: false, | ||
result: [], | ||
pagination, | ||
message: 'Collection is Empty', | ||
}); | ||
} | ||
}; | ||
|
||
module.exports = paginatedList; |
29 changes: 29 additions & 0 deletions
29
backend/src/controllers/appControllers/clientController/read.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,29 @@ | ||
const { migrate } = require('./migrate'); | ||
const mongoose = require('mongoose'); | ||
|
||
const Model = mongoose.model('Client'); | ||
|
||
const read = async (req, res) => { | ||
// Find document by id | ||
let result = await Model.findOne({ _id: req.params.id, removed: false }).exec(); | ||
// If no results found, return document not found | ||
if (!result) { | ||
return res.status(404).json({ | ||
success: false, | ||
result: null, | ||
message: 'No document found by this id: ' + req.params.id, | ||
}); | ||
} else { | ||
// Return success resposne | ||
|
||
const migratedData = migrate(result); | ||
|
||
return res.status(200).json({ | ||
success: true, | ||
result: migratedData, | ||
message: 'we found this document by this id: ' + req.params.id, | ||
}); | ||
} | ||
}; | ||
|
||
module.exports = read; |
51 changes: 51 additions & 0 deletions
51
backend/src/controllers/appControllers/clientController/search.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,51 @@ | ||
const { migrate } = require('./migrate'); | ||
const mongoose = require('mongoose'); | ||
|
||
const Model = mongoose.model('Client'); | ||
|
||
const search = async (req, res) => { | ||
// console.log(req.query.fields) | ||
if (req.query.q === undefined || req.query.q.trim() === '') { | ||
return res | ||
.status(202) | ||
.json({ | ||
success: false, | ||
result: [], | ||
message: 'No document found by this request', | ||
}) | ||
.end(); | ||
} | ||
const fieldsArray = req.query.fields | ||
? req.query.fields.split(',') | ||
: ['name', 'surname', 'birthday']; | ||
|
||
const fields = { $or: [] }; | ||
|
||
for (const field of fieldsArray) { | ||
fields.$or.push({ [field]: { $regex: new RegExp(req.query.q, 'i') } }); | ||
} | ||
// console.log(fields) | ||
|
||
let results = await Model.find(fields).where('removed', false).limit(10).exec(); | ||
|
||
const migratedData = results.map((x) => migrate(x)); | ||
|
||
if (results.length >= 1) { | ||
return res.status(200).json({ | ||
success: true, | ||
result: migratedData, | ||
message: 'Successfully found all documents', | ||
}); | ||
} else { | ||
return res | ||
.status(202) | ||
.json({ | ||
success: false, | ||
result: [], | ||
message: 'No document found by this request', | ||
}) | ||
.end(); | ||
} | ||
}; | ||
|
||
module.exports = search; |
22 changes: 22 additions & 0 deletions
22
backend/src/controllers/appControllers/companyController/index.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,22 @@ | ||
const mongoose = require('mongoose'); | ||
const { modelsFiles } = require('@/models/utils'); | ||
const createCRUDController = require('@/controllers/middlewaresControllers/createCRUDController'); | ||
|
||
const remove = require('./remove'); | ||
|
||
function modelController() { | ||
const modelName = 'Company'; | ||
|
||
if (!modelsFiles.includes(modelName)) { | ||
throw new Error(`Model ${modelName} does not exist`); | ||
} else { | ||
const Model = mongoose.model(modelName); | ||
const methods = createCRUDController(modelName); | ||
|
||
methods.delete = (req, res) => remove(Model, req, res); | ||
|
||
return methods; | ||
} | ||
} | ||
|
||
module.exports = modelController(); |
52 changes: 52 additions & 0 deletions
52
backend/src/controllers/appControllers/companyController/remove.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,52 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const Client = mongoose.model('Client'); | ||
const People = mongoose.model('People'); | ||
|
||
const remove = async (Model, req, res) => { | ||
// cannot delete client it it have one invoice or Client: | ||
// check if client have invoice or quotes: | ||
const { id } = req.params; | ||
console.log('🚀 ~ companyController: remove.js:10 ~ remove ~ id:', id); | ||
|
||
// first find if there alt least one quote or invoice exist corresponding to the client | ||
const client = await Client.findOne({ company: id, removed: false }).exec(); | ||
if (client) { | ||
return res.status(400).json({ | ||
success: false, | ||
result: null, | ||
message: 'Cannot delete company if company attached to any people or she is client', | ||
}); | ||
} | ||
const people = await People.findOne({ company: id, removed: false }).exec(); | ||
if (people) { | ||
return res.status(400).json({ | ||
success: false, | ||
result: null, | ||
message: 'Cannot delete company if company attached to any people or she is client', | ||
}); | ||
} | ||
|
||
// if no People or quote, delete the client | ||
const result = await Model.findOneAndUpdate( | ||
{ _id: id, removed: false }, | ||
{ | ||
$set: { | ||
removed: true, | ||
}, | ||
} | ||
).exec(); | ||
if (!result) { | ||
return res.status(404).json({ | ||
success: false, | ||
result: null, | ||
message: 'No people found by this id: ' + id, | ||
}); | ||
} | ||
return res.status(200).json({ | ||
success: true, | ||
result, | ||
message: 'Successfully Deleted the people by id: ' + id, | ||
}); | ||
}; | ||
module.exports = remove; |
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
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.