Skip to content

Commit

Permalink
fix(api-autoreplies): Added logging to graylog. Autoreply docs have a…
Browse files Browse the repository at this point in the history
… created field now ZMS-127 (#633)

* Update Autoreply information api endpoint added to api docs generation

* Delete Autoreply information api endpoint added to api docs generation

* Request Autoreply information api endpoint added to api docs generation

* autoreply now contains the created field to check when it was created. Added logging to graylog

* make created timestamp second precision. Fix tests

* autoreply.js add optional chaining
  • Loading branch information
NickOvt authored Feb 26, 2024
1 parent f19540f commit f6f5f5e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
50 changes: 44 additions & 6 deletions lib/api/autoreply.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
}
}
}
},
Expand Down Expand Up @@ -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()
});
})
);
Expand Down Expand Up @@ -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')
})
}
}
Expand Down Expand Up @@ -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
});
})
);
Expand Down Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions test/api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -416,7 +417,8 @@ describe('API tests', function () {
text: '',
html: '',
start: false,
end: false
end: false,
created: false
});

r = await server
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -476,7 +482,8 @@ describe('API tests', function () {
text: '',
html: '',
start: false,
end: false
end: false,
created: false
});
});
});
Expand Down

0 comments on commit f6f5f5e

Please sign in to comment.