Skip to content

Commit

Permalink
feat: when adding rp manuel map the names
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafahincal committed Feb 13, 2024
1 parent 9a84324 commit 49e3942
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
18 changes: 14 additions & 4 deletions v1/src/controllers/rps.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,21 @@ const bulkDelete = async (req, res) => {
};

const bulkInsert = async (req, res) => {
console.log('req.body', req.body);
const newRPs = [];
if (req?.body[0]?.siteBound === undefined) {
res.send({
success: false,
message: 'Site Bound ID is required',
});
}
for (let rp of req.body) {
const { name } = await getLastRpBySiteBoundId(rp.siteBound);
const lastRPNumber = parseInt(name.split(' ')[1]);
rp.name = `RP ${lastRPNumber + 1}`;
newRPs.push(rp);
}

// const rp = await getLastRpBySiteBoundId(req.body[0].siteBound);
// console.log('rp', rp);
await bulkInsertRps(req.body);
await bulkInsertRps(newRPs);
res.send({
success: true,
message: 'Created successfully',
Expand Down
10 changes: 5 additions & 5 deletions v1/src/services/rp.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ const getRpsBySiteBoundId = async (siteBoundId) => {
};

const getLastRpBySiteBoundId = async (siteBoundId) => {
console.log('siteBoundId', siteBoundId);
const rp = await Rp.find({
const rp = await Rp.findOne({
siteBound: siteBoundId,
}).limit(1);
console.log('rp2222', rp);
if (rp) return rp[0];
})
.sort({ name: -1 })
.limit(1);
if (rp) return rp;
throw new Error('Rps not found');
};

Expand Down

0 comments on commit 49e3942

Please sign in to comment.