Skip to content

Commit

Permalink
Merge pull request #140 from osamhack2021/feature/backend/#139
Browse files Browse the repository at this point in the history
Feature/backend/#139
  • Loading branch information
134130 authored Oct 5, 2021
2 parents ed3358d + f7be416 commit cfb7c43
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
10 changes: 10 additions & 0 deletions backend/algolia/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,14 @@ const ALGOLIA_INDEX_NAME = process.env.ALGOLIA_INDEX_NAME;
const client = algoliasearch(ALGOLIA_APP_ID, ALGOLIA_ADMIN_API_KEY);
const index = client.initIndex(ALGOLIA_INDEX_NAME);

index.setSettings({
searchableAttributes: [
'title',
'type',
'tags',
'status',
'comments'
]
});

module.exports = index;
17 changes: 12 additions & 5 deletions backend/controllers/itemController.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ module.exports = {
try {
const query = req.params.query;

const result = await algolia.search(query);
const result = await algolia.search(query, {
filters: `status:"modified" AND NOT accessGroups.read:"${res.locals.group}"`
});

if(result.hits.length < 1) throw new NotFoundError('Not Found');

Expand Down Expand Up @@ -75,6 +77,7 @@ module.exports = {

const result = await itemService.create(body);

// Add object to Algolia
let object = result.toObject();
object.objectID = object._id;
delete object._id;
Expand All @@ -97,14 +100,18 @@ module.exports = {
if(item === null) throw new NotFoundError(`Not Found: No result is found for item_id: ${item_id}`);

// Check session's edit authority
const user = await userService.searchByServiceNumber(res.locals.serviceNumber);
//if(!item.accessGroups.edit.some(i => i.equals(user.group)))
// throw new ForbiddenError(`Forbidden: You are not in editable group`);
if(!item.accessGroups.edit.some(i => i.equals(res.locals.group)))
throw new ForbiddenError(`Forbidden: You are not in editable group`);

// Append Contributor
item = Object.assign(item, { contributors: [...item.contributors, res.locals._id] });

await itemService.update(item, req.body);
const result = await itemService.update(item, req.body);

// Update object to Algolia
let object = result.toObject();
object.objectID = object._id;
delete object._id;

res.status(204).send();
} catch(err) {
Expand Down
2 changes: 1 addition & 1 deletion backend/models/Item.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ itemSchema.pre('save', function(next) {
next();
});

itemSchema.pre('updateOne', function(next) {
itemSchema.pre('findOneAndUpdate', function(next) {
const data = this.getUpdate();

let keys = ['contributors', 'history'];
Expand Down
1 change: 1 addition & 0 deletions backend/services/authService.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ module.exports = {
const token = jwt.sign({
_id: loginUser._id,
serviceNumber: loginUser.serviceNumber,
group: loginUser.group,
status: loginUser.status,
}, SECRET_KEY, {
expiresIn: '1h'
Expand Down
6 changes: 4 additions & 2 deletions backend/services/itemService.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,12 @@ module.exports = {
// Clear inspection
payload.inspection = {};

// Update item
await Item.findOneAndUpdate(item, payload)
delete payload._id;

// Update item
const result = await Item.findOneAndUpdate({ _id: item._id }, payload);
return result;

} catch(err) {
throw new BusinessError(err.message);
}
Expand Down

0 comments on commit cfb7c43

Please sign in to comment.