Skip to content

Commit

Permalink
feat(apis): save in db for the first time :) ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil committed Mar 15, 2020
1 parent cea28f2 commit 787acf6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
5 changes: 4 additions & 1 deletion modules/apis/models/apis.model.mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ const Schema = mongoose.Schema;
* Data Model Mongoose
*/
const ApiMongoose = new Schema({
title: String,
title: {
type: String,
unique: 'Title already exists',
},
url: String,
auth: String,
serviceId: String,
Expand Down
33 changes: 33 additions & 0 deletions modules/apis/repositories/apis.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,36 @@ exports.update = (api) => new Api(api).save().then((a) => a.populate('history').
* @return {Object} confirmation of delete
*/
exports.delete = (api) => Api.deleteOne({ _id: api.id }).exec();

/**
* @desc Function to import list of locations in db
* @param {Object} locations
* @return {Object} locations
*/
exports.import = (collection, items, filters) => {
const _schema = new mongoose.Schema({}, {
collection,
strict: false,
});

let model;
try {
model = mongoose.model(collection);
} catch (error) {
model = mongoose.model(collection, _schema);
}

return model.bulkWrite(items.map((item) => {
const filter = {};
filters.forEach((value) => {
filter[value] = item[value];
});
return {
updateOne: {
filter,
update: item,
upsert: true,
},
};
}));
};
4 changes: 2 additions & 2 deletions modules/apis/services/apis.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Module dependencies
*/
const path = require('path');
const _ = require('lodash');

const UserService = require(path.resolve('./modules/users/services/user.service.js'));
const montaineMapping = require(path.resolve('./lib/helpers/montaineMapping'));
Expand Down Expand Up @@ -113,10 +114,9 @@ exports.load = async (api) => {
// prepare for save
if (result.result) {
result.result = montaineSaving.saving(result.result, start);
await ApisRepository.import(_.camelCase(api.title), result.result, ['@id', '@date']);
}

// Mapping

const history = await HistoryRepository.create(montaineRequest.setScrapHistory(result.request, api, start));
api.status = result.request.type === 'success';
api.history.push(history._id);
Expand Down

0 comments on commit 787acf6

Please sign in to comment.