Skip to content

Commit

Permalink
autoreply now contains the created field to check when it was created…
Browse files Browse the repository at this point in the history
…. Added logging to graylog
  • Loading branch information
NickOvt committed Feb 22, 2024
1 parent 50f80ec commit d6d6307
Showing 1 changed file with 42 additions and 6 deletions.
48 changes: 42 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,30 @@ module.exports = (db, server) => {
}
}

const r = await db.database.collection('autoreplies').updateOne({ user }, { $set: result.value }, { upsert: true });
result.value.created = new Date();

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 +181,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 +233,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()
});
})
);
Expand Down Expand Up @@ -271,7 +298,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

0 comments on commit d6d6307

Please sign in to comment.