-
Notifications
You must be signed in to change notification settings - Fork 891
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 #83 from rairprotocol/80-categories
Category config and login message
- Loading branch information
Showing
10 changed files
with
258 additions
and
11 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
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,26 @@ | ||
const { Router } = require('express'); | ||
const { | ||
updateCategories, | ||
getCategories, | ||
} = require('./categories.Service'); | ||
const { requireUserSession } = require('../../middleware/verifyUserSession'); | ||
const validation = require('../../middleware/validation'); | ||
const { verifySuperAdmin } = require('../../middleware'); | ||
|
||
const router = Router(); | ||
|
||
router.get( | ||
'/', | ||
getCategories, | ||
); | ||
|
||
router.post( | ||
'/', | ||
validation(['dbCategory'], 'body'), | ||
requireUserSession, | ||
verifySuperAdmin, | ||
updateCategories, | ||
getCategories, | ||
); | ||
|
||
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,63 @@ | ||
const { Category, File } = require('../../models'); | ||
const AppError = require('../../utils/errors/AppError'); | ||
|
||
module.exports = { | ||
getCategories: async (req, res, next) => { | ||
try { | ||
const list = await Category.aggregate([{ | ||
$lookup: { | ||
from: 'File', | ||
localField: '_id', | ||
foreignField: 'category', | ||
as: 'files', | ||
}, | ||
}, { | ||
$project: { | ||
_id: 1, | ||
name: 1, | ||
files: { | ||
$size: '$files', | ||
}, | ||
}, | ||
}]); | ||
return res.json({ result: list, success: true }); | ||
} catch (error) { | ||
return next(new AppError(error)); | ||
} | ||
}, | ||
updateCategories: async (req, res, next) => { | ||
try { | ||
const currentList = await Category.find().lean(); | ||
const { list } = req.body; | ||
// eslint-disable-next-line no-restricted-syntax | ||
for await (const category of currentList) { | ||
const update = list.find((item) => item?._id === category._id.toString()); | ||
if (update) { | ||
await Category.findByIdAndUpdate(update._id, { $set: { name: update.name } }); | ||
} else { | ||
const usingCategory = await File.findOne({ | ||
category: category._id, | ||
}); | ||
if (!usingCategory) { | ||
await Category.findByIdAndDelete(category._id); | ||
} | ||
} | ||
} | ||
// eslint-disable-next-line no-restricted-syntax | ||
for await (const category of list) { | ||
if (category._id) { | ||
// eslint-disable-next-line no-continue | ||
continue; | ||
} else { | ||
const newCat = new Category({ | ||
name: category.name, | ||
}); | ||
await newCat.save(); | ||
} | ||
} | ||
return next(); | ||
} catch (error) { | ||
return next(new AppError(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
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