2020-03-25 18:27:58 +00:00
|
|
|
const { v4: uuidv4 } = require('uuid');
|
|
|
|
|
|
|
|
class PublicStateController {
|
|
|
|
|
|
|
|
constructor(database) {
|
|
|
|
this.database = database;
|
|
|
|
};
|
|
|
|
|
|
|
|
create(data, user, onSuccess, onError) {
|
|
|
|
let collection = this.database.collection('publicStates');
|
|
|
|
collection.findOne({name: data.name}, {}, (dbErr, dbRes) => {
|
|
|
|
if (dbErr === null) {
|
|
|
|
if (dbRes === null) {
|
|
|
|
let title = data.tile | data.name;
|
|
|
|
let pubState = {
|
|
|
|
id: uuidv4(),
|
|
|
|
name: data.name,
|
|
|
|
title: title,
|
|
|
|
createdBy: user.id,
|
|
|
|
ts: new Date().getTime(),
|
|
|
|
v: 1
|
|
|
|
};
|
|
|
|
collection.insertOne(pubState, (insertErr, insertRes) => {
|
|
|
|
if (insertErr === null) {
|
2020-03-25 22:40:23 +00:00
|
|
|
console.log('call onSuccess with data: ' + JSON.stringify(pubState));
|
2020-03-25 18:27:58 +00:00
|
|
|
onSuccess(pubState);
|
|
|
|
} else {
|
|
|
|
onError({
|
|
|
|
error: 'database error: could not create public state',
|
|
|
|
message: insertErr.message
|
|
|
|
})
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
onError({
|
|
|
|
code: 'app error',
|
|
|
|
message: 'a public state with that name already exists'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
onError({
|
|
|
|
code: 'database error',
|
|
|
|
message: dbErr.message
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2020-03-29 16:21:36 +00:00
|
|
|
|
2020-03-25 18:27:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = PublicStateController;
|