2018-04-29 20:33:27 +00:00
|
|
|
const map = require('lodash/map');
|
2018-04-27 15:13:18 +00:00
|
|
|
const retry = require('async-retry');
|
2018-04-28 07:24:53 +00:00
|
|
|
const generate = require('nanoid/generate');
|
2018-04-27 15:13:18 +00:00
|
|
|
const markdown = require('markdown').markdown;
|
2018-04-28 07:24:53 +00:00
|
|
|
const config = require('../../config');
|
2018-03-22 15:45:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Code
|
|
|
|
{
|
|
|
|
constructor(values)
|
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
self.id = values.id || values._id || null;
|
|
|
|
self.userId = values.userId || null;
|
|
|
|
self.created = values.created || new Date();
|
|
|
|
self.expiration = values.expiration || null;
|
2018-04-27 15:13:18 +00:00
|
|
|
self.description = values.description || null;
|
|
|
|
self.message = values.message || null;
|
|
|
|
self.messageHTML = values.messageHTML || null;
|
2018-03-22 15:45:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class CodeRepository
|
2018-03-21 15:51:46 +00:00
|
|
|
{
|
|
|
|
constructor(store)
|
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
self.store = store;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
init()
|
2018-03-21 15:51:46 +00:00
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
2018-04-27 15:13:18 +00:00
|
|
|
resolve();
|
2018-03-21 15:51:46 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-28 08:18:27 +00:00
|
|
|
getUserId(code)
|
2018-03-21 15:51:46 +00:00
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
|
|
|
self.store.findOne({ _id: code }, (err, doc) =>
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(doc !== null ? doc.userId : null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-28 08:18:27 +00:00
|
|
|
async insert(code)
|
2018-03-21 15:51:46 +00:00
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return await retry(async bail =>
|
|
|
|
{
|
2018-04-28 07:24:53 +00:00
|
|
|
var codeId = generate(config.code.alphabet, config.code.length);
|
2018-03-21 15:51:46 +00:00
|
|
|
|
2018-04-27 15:13:18 +00:00
|
|
|
if ((await self.findCodeUserId(codeId)) !== null)
|
|
|
|
throw new Error('Code ' + codeId + ' already exists');
|
2018-03-21 15:51:46 +00:00
|
|
|
|
|
|
|
self.store.insert({
|
2018-04-27 15:13:18 +00:00
|
|
|
_id: codeId,
|
|
|
|
userId: code.userId,
|
|
|
|
created: code.created || new Date(),
|
|
|
|
expiration: code.expiration,
|
|
|
|
description: code.description,
|
|
|
|
message: code.message,
|
|
|
|
messageHTML: self.getMessageHTML(code.message)
|
2018-03-21 15:51:46 +00:00
|
|
|
})
|
|
|
|
|
2018-04-27 15:13:18 +00:00
|
|
|
return codeId;
|
2018-03-21 15:51:46 +00:00
|
|
|
}, {
|
|
|
|
retries: 100,
|
|
|
|
minTimeout: 0,
|
|
|
|
maxTimeout: 0
|
|
|
|
});
|
|
|
|
}
|
2018-03-22 15:45:11 +00:00
|
|
|
|
|
|
|
|
2018-04-28 08:18:27 +00:00
|
|
|
update(code)
|
2018-04-27 15:13:18 +00:00
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
|
|
|
self.store.update({ _id: code.id }, { $set: {
|
|
|
|
expiration: code.expiration,
|
|
|
|
description: code.description,
|
|
|
|
message: code.message,
|
|
|
|
messageHTML: self.getMessageHTML(code.message)
|
|
|
|
}},
|
|
|
|
(err, numAffected) =>
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numAffected == 0)
|
|
|
|
{
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-28 08:18:27 +00:00
|
|
|
list(userId)
|
2018-03-22 15:45:11 +00:00
|
|
|
{
|
2018-04-26 12:08:08 +00:00
|
|
|
var self = this;
|
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
2018-04-26 12:08:08 +00:00
|
|
|
self.store.find(userId != null ? { userId: userId } : {}, (err, docs) =>
|
2018-03-22 15:45:11 +00:00
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-29 20:33:27 +00:00
|
|
|
resolve(map(docs, (doc) => new Code(doc)));
|
2018-03-22 15:45:11 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-04-27 15:13:18 +00:00
|
|
|
|
|
|
|
|
2018-04-28 07:24:53 +00:00
|
|
|
getDescriptions()
|
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
|
|
|
self.store.find({}, (err, docs) =>
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var descriptions = {};
|
|
|
|
docs.forEach((dbCode) =>
|
|
|
|
{
|
|
|
|
if (dbCode.description)
|
|
|
|
descriptions[dbCode._id] = dbCode.description;
|
|
|
|
});
|
|
|
|
|
|
|
|
resolve(descriptions);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-28 08:18:27 +00:00
|
|
|
get(codeId)
|
2018-04-27 15:13:18 +00:00
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
|
|
|
self.store.findOne({ _id: codeId }, (err, doc) =>
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(doc !== null ? new Code(doc) : null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
getMessageHTML(message)
|
|
|
|
{
|
|
|
|
return message !== null ? markdown.toHTML(message) : null;
|
|
|
|
}
|
2018-04-28 07:51:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
delete(code)
|
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
|
|
|
self.store.remove({ _id: code }, (err, numRemoved) =>
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-03-21 15:51:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
module.exports = {
|
|
|
|
Code,
|
|
|
|
CodeRepository
|
|
|
|
}
|