diff --git a/lib/api/autoreply.js b/lib/api/autoreply.js index 033f9759..9b5bc4ec 100644 --- a/lib/api/autoreply.js +++ b/lib/api/autoreply.js @@ -44,7 +44,13 @@ module.exports = (db, server) => { user: userId }, response: { - 200: { description: 'Success', model: Joi.object({ success: successRes, id: Joi.string().required().description('Autoreply ID') }) } + 200: { + description: 'Success', + model: Joi.object({ + success: successRes, + id: Joi.string().required().description('Autoreply ID') + }) + } } } }, @@ -109,11 +115,32 @@ module.exports = (db, server) => { } } - const r = await db.database.collection('autoreplies').updateOne({ user }, { $set: result.value }, { upsert: true }); + result.value.created = new Date(); + result.value.created.setMilliseconds(0); + result.value.created = result.value.created.toISOString(); + + const autoreplyData = await db.database + .collection('autoreplies') + .findOneAndUpdate({ user }, { $set: result.value }, { returnDocument: 'after', upsert: true }); + + server.loggelf({ + short_message: `[AUTOREPLY] ${autoreplyData.lastErrorObject?.upserted ? 'create' : 'update'}`, + _mail_action: `${autoreplyData.lastErrorObject?.upserted ? 'Create' : 'Update'} autoreply`, + _user: user, + _autoreply_id: autoreplyData.value?._id.toString(), + _autoreply_status: autoreplyData.value?.status, + _autoreply_name: autoreplyData.value?.name, + _autoreply_subject: autoreplyData.value?.subject, + _autoreply_start: autoreplyData.value?.start, + _autoreply_end: autoreplyData.value?.end, + _autoreply_created: autoreplyData.value?.created, + _sess: result.value.sess, + _ip: result.value.ip + }); return res.json({ success: true, - id: ((r.upsertedId && r.upsertedId._id) || '').toString() + id: autoreplyData.value._id.toString() }); }) ); @@ -156,7 +183,8 @@ module.exports = (db, server) => { .empty('') .allow(false) .description('Datestring of the start of the autoreply or boolean false to disable start checks'), - end: Joi.date().empty('').allow(false).description('Datestring of the end of the autoreply or boolean false to disable end checks') + end: Joi.date().empty('').allow(false).description('Datestring of the end of the autoreply or boolean false to disable end checks'), + created: Joi.date().required().description('Datestring of when the Autoreply was created') }) } } @@ -207,7 +235,8 @@ module.exports = (db, server) => { text: entry.text || '', html: entry.html || '', start: entry.start || false, - end: entry.end || false + end: entry.end || false, + created: entry.created || entry._id?.getTimestamp() || false }); }) ); @@ -271,7 +300,16 @@ module.exports = (db, server) => { }); } - await db.database.collection('autoreplies').deleteOne({ user }); + const autoreplyData = await db.database.collection('autoreplies').findOneAndDelete({ user }, { projection: { _id: true } }); + + server.loggelf({ + short_message: '[AUTOREPLY] Delete', + _mail_action: 'Delete autoreply', + _user: user, + _autoreply_id: autoreplyData.value?._id.toString(), + _sess: result.value.sess, + _ip: result.value.ip + }); return res.json({ success: true diff --git a/test/api-test.js b/test/api-test.js index 02130ff3..53585f09 100644 --- a/test/api-test.js +++ b/test/api-test.js @@ -11,6 +11,7 @@ chai.config.includeStack = true; const config = require('wild-config'); const server = supertest.agent(`http://127.0.0.1:${config.api.port}`); +const ObjectId = require('mongodb').ObjectId; describe('API tests', function () { let userId, asp, address, inbox; @@ -416,7 +417,8 @@ describe('API tests', function () { text: '', html: '', start: false, - end: false + end: false, + created: false }); r = await server @@ -432,6 +434,8 @@ describe('API tests', function () { .expect(200); expect(r.body.success).to.be.true; + const autoreplyId = new ObjectId(r.body._id); + r = await server.get(`/users/${userId}/autoreply`).expect(200); expect(r.body).to.deep.equal({ success: true, @@ -441,7 +445,8 @@ describe('API tests', function () { text: 'Away from office until Dec.19', html: '', start: '2017-11-15T00:00:00.000Z', - end: '2017-12-19T00:00:00.000Z' + end: '2017-12-19T00:00:00.000Z', + created: autoreplyId.getTimestamp().toISOString() }); r = await server @@ -463,7 +468,8 @@ describe('API tests', function () { text: 'Away from office until Dec.19', html: '', start: false, - end: '2017-12-19T00:00:00.000Z' + end: '2017-12-19T00:00:00.000Z', + created: autoreplyId.getTimestamp().toISOString() }); await server.delete(`/users/${userId}/autoreply`).expect(200); @@ -476,7 +482,8 @@ describe('API tests', function () { text: '', html: '', start: false, - end: false + end: false, + created: false }); }); });