mirror of
				https://gitlab.com/Alamantus/Readlebee.git
				synced 2025-11-04 02:07:11 +01:00 
			
		
		
		
	Create default shelves in db on account creation
This commit is contained in:
		
							parent
							
								
									da2425a73d
								
							
						
					
					
						commit
						53662c0a60
					
				
					 3 changed files with 111 additions and 24 deletions
				
			
		| 
						 | 
				
			
			@ -133,6 +133,7 @@ class Account {
 | 
			
		|||
  async createUser (email, username, displayName, password, needsConfirmation) {
 | 
			
		||||
    const hashData = Account.hashPassword(password);
 | 
			
		||||
    // The data should already have gone through Account.cleanCreateAccountFormData()
 | 
			
		||||
    try {
 | 
			
		||||
      return await this.model.create({
 | 
			
		||||
        email,
 | 
			
		||||
        username,
 | 
			
		||||
| 
						 | 
				
			
			@ -141,6 +142,11 @@ class Account {
 | 
			
		|||
        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();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										68
									
								
								server/controllers/shelf.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								server/controllers/shelf.js
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -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;
 | 
			
		||||
| 
						 | 
				
			
			@ -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,10 +32,21 @@ 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') {
 | 
			
		||||
    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) {
 | 
			
		||||
      try {
 | 
			
		||||
        const file = fs.readFileSync(path.resolve(__dirname, '../templates/email.confirm_account.txt'));
 | 
			
		||||
| 
						 | 
				
			
			@ -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',
 | 
			
		||||
      });
 | 
			
		||||
    }
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  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',
 | 
			
		||||
    });
 | 
			
		||||
    }
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  fastify.post('/api/account/login', async (request, reply) => {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue