diff --git a/server/controllers/account.js b/server/controllers/account.js index 54434f5..1d08eed 100644 --- a/server/controllers/account.js +++ b/server/controllers/account.js @@ -133,14 +133,20 @@ class Account { async createUser (email, username, displayName, password, needsConfirmation) { const hashData = Account.hashPassword(password); // The data should already have gone through Account.cleanCreateAccountFormData() - return await this.model.create({ - email, - username, - displayName, - passwordHash: hashData.hash, - passwordSalt: hashData.salt, - accountConfirm: needsConfirmation ? crypto.randomBytes(32).toString('hex') : null, - }); + try { + return await this.model.create({ + email, + username, + displayName, + passwordHash: hashData.hash, + passwordSalt: hashData.salt, + accountConfirm: needsConfirmation ? crypto.randomBytes(32).toString('hex') : null, + }); + } catch (error) { + return { + error, + }; + } } async confirmUser (id, accountConfirm) { @@ -210,6 +216,10 @@ class Account { id: existingUser.id, }; } + + async deleteUser(user) { + return await user.destroy(); + } } diff --git a/server/controllers/shelf.js b/server/controllers/shelf.js new file mode 100644 index 0000000..932e0d3 --- /dev/null +++ b/server/controllers/shelf.js @@ -0,0 +1,68 @@ +class Shelf { + constructor (shelfModel, shelfItemModel) { + this.model = shelfModel; + this.itemModel = shelfItemModel; + } + + async createDefaultShelves (user) { + try { + const defaultShelvesCreated = await this.model.bulkCreate([ + { + userId: user.id, + name: 'Reading', + isDeletable: false, + }, + { + userId: user.id, + name: 'Want to Read', + isDeletable: false, + }, + { + userId: user.id, + name: 'Finished', + isDeletable: false, + }, + { + userId: user.id, + name: 'Did Not Finish', + isDeletable: false, + } + ]); + + if (defaultShelvesCreated.some(result => !result)) { + return { + error: true, + shelfResults: defaultShelvesCreated, + }; + } + + return defaultShelvesCreated; + } catch (error) { + return { + error, + } + } + } + + async getLastUpdatedTimestamp (shelf) { + const lastEditedItem = await this.itemModel.findOne({ + attributes: ['updatedAt'], + where: { + shelf: shelf.id, + }, + order: [ + [ + 'updatedAt', + 'DESC' + ], + ], + }); + + if (lastEditedItem && lastEditedItem.updatedAt > shelf.updatedAt) { + return lastEditedItem.updatedAt; + } + return shelf.updatedAt; + } +} + +module.exports = Shelf; \ No newline at end of file diff --git a/server/routes/account.js b/server/routes/account.js index 53da987..fd6a55b 100644 --- a/server/routes/account.js +++ b/server/routes/account.js @@ -1,6 +1,7 @@ const fs = require('fs'); const path = require('path'); const Account = require('../controllers/account'); +const Shelf = require('../controllers/shelf'); async function routes(fastify, options) { fastify.get('/api/accounts/test', async (request, reply) => { @@ -31,8 +32,19 @@ async function routes(fastify, options) { const newUser = await account.createUser(formData.email, formData.username, formData.displayName, formData.password, fastify.canEmail); - if (typeof newUser.error !== 'undefined') { - return reply.code(400).send(newUser); + if (typeof newUser.error !== 'undefined' && newUser.error !== false) { + newUser.message = 'api.account_create_fail'; + return reply.code(400).send(newUser); + } + + const shelf = new Shelf(fastify.models.Shelf, fastify.models.ShelfItem); + const defaultShelvesCreated = await shelf.createDefaultShelves(newUser); + + // If some of the default shelves are not created successfully, delete the user and send an error + if (typeof defaultShelvesCreated.error !== 'undefined' && defaultShelvesCreated.error !== false) { + account.deleteUser(newUser); + defaultShelvesCreated.message = 'api.account_create_fail'; + return reply.code(400).send(defaultShelvesCreated); } if (fastify.canEmail) { @@ -69,20 +81,16 @@ async function routes(fastify, options) { error: false, message: 'api.account_confirm_email', }); - }) + }); } catch (ex) { console.error(ex); - return reply.send({ - error: false, - message: 'api.account_create_success', - }); } - } else { - return reply.send({ - error: false, - message: 'api.account_create_success', - }); } + + return reply.send({ + error: false, + message: 'api.account_create_success', + }); }); fastify.post('/api/account/confirm', async (request, reply) => { @@ -140,11 +148,12 @@ async function routes(fastify, options) { }) } catch (ex) { console.error(ex); - return reply.send({ - error: false, - message: 'api.account_confirm_success', - }); } + + return reply.send({ + error: false, + message: 'api.account_confirm_success', + }); }); fastify.post('/api/account/login', async (request, reply) => {