All files / Zapcard_API mock_ios.js

0% Statements 0/45
0% Branches 0/4
0% Functions 0/2
0% Lines 0/45
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79                                                                                                                                                             
/**
* This file is a mock file to pre-generate some data in to the application
*/
/**
* Import Relationship Model
*/
const Relationship = require('./models/relationship');
const UserService = require('./services/user.service');
const CardController = require('./controllers/card');
const User = require('./models/user');
const CardMock = require('./test/mockData/card.mock');
const UserMock = require('./test/mockData/user.mock');
/**
* Dependencies and config
*/
const path = require('path');
const mongoose = require('mongoose');
const config = require('./config');
 
const runIOSMock = async () => {
  try {
    mongoose.connect(`${config.db.uri}`, { useMongoClient: true });
    await User.remove({});
    await Relationship.remove({});
    const { reqs } = CardMock;
    const authenticatedUser1 = await User.create(UserMock.user1);
    const authenticatedUser2 = await User.create(UserMock.user2);
    const authenticatedUser3 = await User.create(UserMock.user3);
    const filePath1 = { path: path.resolve('./test/mockData/img/Ralfs_Lagzda.png') };
    const filePath2 = { path: path.resolve('./test/mockData/img/Karlis_Filipsons.png') };
    const filePath3 = { path: path.resolve('./test/mockData/img/Elina_Baumane.png') };
    await UserService.modifyProfilePicture(authenticatedUser1, filePath1);
    await UserService.modifyProfilePicture(authenticatedUser2, filePath2);
    await UserService.modifyProfilePicture(authenticatedUser3, filePath3);
 
    const relationship1 = {
      sharingUser: authenticatedUser1.id,
      receivingUser: authenticatedUser2.id,
      sharedCards: [],
      createdBy: authenticatedUser1.id,
    };
    const relationship2 = {
      sharingUser: authenticatedUser2.id,
      receivingUser: authenticatedUser1.id,
      sharedCards: [],
      createdBy: authenticatedUser1.id,
    };
    const relationship3 = {
      sharingUser: authenticatedUser3.id,
      receivingUser: authenticatedUser1.id,
      sharedCards: [],
      createdBy: authenticatedUser1.id,
    };
 
    await Promise.all(reqs.map(async (req, index) => {
      if (index > 7) {
        req.params.userId = authenticatedUser3.id;
        const card = await CardController.create(req);
        relationship3.sharedCards.push(card);
      } else if (index > 5) {
        req.params.userId = authenticatedUser2.id;
        const card = await CardController.create(req);
        relationship2.sharedCards.push(card);
      } else {
        req.params.userId = authenticatedUser1.id;
        const card = await CardController.create(req);
        relationship1.sharedCards.push(card);
      }
    }));
    await Relationship.create(relationship1);
    await Relationship.create(relationship2);
    await Relationship.create(relationship3);
    mongoose.connection.close();
  } catch (err) {
    throw err;
  }
};
runIOSMock();