Skip to content
This repository has been archived by the owner on Nov 17, 2024. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sugrado committed Jun 22, 2024
2 parents d144ac1 + 3ad6c94 commit e3219ec
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
38 changes: 38 additions & 0 deletions v1/src/controllers/rps.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {
bulkDeleteRps,
insertRp,
updateRp,
getRp,
getRpsBySiteBoundId,
bulkInsertRps,
getLastRpBySiteBoundId,
Expand All @@ -14,6 +15,7 @@ const {
const {
calculateDistributionCurves,
} = require('../services/distributionCurves.service');
const { getDiscsByRpId, insertDisc } = require('../services/rpDisc.service');

const create = async (req, res) => {
const { name } = await getLastRpBySiteBoundId(req.body.siteBound);
Expand All @@ -29,6 +31,41 @@ const create = async (req, res) => {
});
};

const copyPaste = async (req, res) => {
const { siteBoundId } = req.params;
const { rpId } = req.body;

const { name } = await getLastRpBySiteBoundId(siteBoundId);
const lastRPNumber = parseInt(name.split(' ')[1]);

const rpToCopy = await getRp(rpId);
const cleanRP = JSON.parse(JSON.stringify(rpToCopy));
const { _id, createdAt, updatedAt, ...cleanRPData } = cleanRP;

const pastedRp = await insertRp({
...cleanRPData,
name: `RP ${lastRPNumber + 1}`,
siteBound: siteBoundId,
});

const rpDiscs = await getDiscsByRpId(rpId);
const cleanRpDiscs = JSON.parse(JSON.stringify(rpDiscs));
for (let rpDisc of cleanRpDiscs) {
const { _id, createdAt, updatedAt, ...cleanRpDisc } = rpDisc;

await insertDisc({
...cleanRpDisc,
rpId: pastedRp._id,
});
}

res.send({
pastedRp,
success: true,
message: 'Rp pasted successfully',
});
};

const update = async (req, res) => {
const { rpId } = req.params;
const rp = await updateRp(rpId, req.body);
Expand Down Expand Up @@ -148,4 +185,5 @@ module.exports = {
exportBySiteBoundToExcel,
getExcelTemplate,
importFromXlsx,
copyPaste,
};
2 changes: 2 additions & 0 deletions v1/src/routes/rps.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
exportBySiteBoundToExcel,
getExcelTemplate,
importFromXlsx,
copyPaste,
} = require('../controllers/rps.controller');
const errorCatcher = require('../scripts/utils/errorCatcher');
const rpValidation = require('../validations/rp.validation');
Expand All @@ -19,6 +20,7 @@ const router = express.Router();

router.route('/').get(errorCatcher(index));
router.route('/:rpId').put(errorCatcher(update));
router.route(`/copy-paste/:siteBoundId`).post(errorCatcher(copyPaste));
router.route('/:siteBoundId').get(errorCatcher(getBySiteBoundId));
router.route('/').post(errorCatcher(create));
router.route('/bulk-delete').post(errorCatcher(bulkDelete));
Expand Down
8 changes: 8 additions & 0 deletions v1/src/services/rp.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ const list = async () => {
if (rps) return rps;
throw new Error('Rps not found');
};

const get = async (rpId) => {
const rp = await Rp.findById(rpId);
if (rp) return rp;
throw new Error('Rp not found');
};

const bulkDeleteRps = async (rps) => {
const result = await Rp.deleteMany({ _id: { $in: rps } });
console.log('silme sonucu: ', result);
Expand Down Expand Up @@ -163,6 +170,7 @@ module.exports = {
listRps: list,
insertRp: insert,
updateRp: update,
getRp: get,
getRpsBySiteBoundId,
bulkInsertRps,
getLastRpBySiteBoundId,
Expand Down

0 comments on commit e3219ec

Please sign in to comment.