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) { console.log('call onSuccess with data: ' + JSON.stringify(pubState)); 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 }); } }); }; }; module.exports = PublicStateController;