Skip to content

Commit 62e0598

Browse files
committed
prepare history and submitData
1 parent 84ea942 commit 62e0598

File tree

4 files changed

+681
-7
lines changed

4 files changed

+681
-7
lines changed

controllers/sync.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
const asyncHandler = require('@evnotify/utils').asyncHandler;
2-
const SettingsModel = require('../models/Settings');
2+
const HistoryModel = require('../models/History');
3+
const SyncModel = require('../models/Sync');
34
const errors = require('../errors.json');
45

5-
const deleteSettings = asyncHandler(async(req, res, next) => {
6-
await SettingsModel.deleteOne({
7-
akey: req.params.akey
8-
});
6+
const submitData = asyncHandler(async(req, res, next) => {
7+
// TODO outsource this in @evnotify/utils?!
8+
if (req.params.akey !== req.headers.AKey) return next(errors.AKEY_MISMATCH);
9+
10+
const syncObj = req.body;
911

10-
res.status(204).end();
12+
syncObj.akey = req.params.akey;
13+
// create a history record
14+
await HistoryModel.create(syncObj);
15+
// sync to log?! trigger
16+
// update last sync
17+
res.json(await SyncModel.updateOne({
18+
akey: req.params.akey
19+
}, req.body));
1120
});
1221

1322
module.exports = {

models/History.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const mongoose = require('mongoose');
2+
const connection = require('@evnotify/utils').db.getDB();
3+
4+
const TelemetrySchema = require('./Telemetry');
5+
const LocationSchema = require('./Location');
6+
7+
const options = {
8+
id: false,
9+
collection: 'sync',
10+
timestamps: true,
11+
toObject: {
12+
getters: true
13+
},
14+
versionKey: false
15+
};
16+
17+
const HistorySchema = new mongoose.Schema({
18+
akey: {
19+
type: String,
20+
required: true
21+
},
22+
telemetry: TelemetrySchema,
23+
location: LocationSchema
24+
}, options);
25+
26+
module.exports = connection.model('History', HistorySchema);

0 commit comments

Comments
 (0)