aboutsummaryrefslogtreecommitdiff
path: root/populateDb.ts
blob: e25005d61a3ef6938ac44ed21e70d2ed6202fdd5 (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import mongoose from 'mongoose';
import bluebird from 'bluebird';
import _ from 'lodash';
import {
  User,
  Poll,
  Vote,
  Feedback
} from 'which-types';

import app from './app';

const MONGODB_URL = process.env.MONGODB_URI || 'mongodb://localhost:27017/which';

mongoose.connect(MONGODB_URL, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
  useCreateIndex: true,
  useFindAndModify: false,
  family: 4 // Use IPv4, skip trying IPv6
});

const POLLS_AMOUNT = 20;

const imageUrls: string[] = [
  // eslint-disable max-len
  'https://cdn.psychologytoday.com/sites/default/files/field_blog_entry_images/2019-06/pexels-photo-556667.jpeg',
  'https://images.pexels.com/photos/556666/pexels-photo-556666.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500',
  'https://i.pinimg.com/originals/50/91/3e/50913eeb04768a5b1fa9985c16704d96.jpg',
  'https://grazia.wwmindia.com/photogallery/2017/apr/1_1491461089.jpg'
];

const names: string[] = [
  'Emma',
  'Elise',
  'Jack',
  'Oliver',
  'Jamie',
  'Adam',
  'Jordan',
  'William'
];

const choices = [
  'left',
  'right'
];


const createPoll = (authorId: string): Promise<Poll> => {
  const generateImageData = () => ({
    url: _.sample(imageUrls) || ''
  });

  return app.service('polls').create({
    contents: {
      left: generateImageData(),
      right: generateImageData()
    },
    authorId
  });
};

const createUser = (username: string): Promise<User> => {
  return app.service('users').create({
    avatarUrl: _.sample(imageUrls) || '',
    password: 'supersecret',
    username
  });
};

const createVote = (userId: string, pollId: string): Promise<Vote> => {
  return app.service('votes').create({
    pollId,
    which: _.sample(choices)
  }, { user: { _id: userId }, authenticated: true });
};

const createFeedback = (userId: string): Promise<Feedback> => {
  return app.service('feedback').create({
    version: 'v1.0.0',
    score: _.sample([1, 2, 3, 4, 5]),
    content: 'Absolutely amazing!'
  }, { user: { _id: userId }, authenticated: true });
};

const populate = async () => {
  const users = await bluebird.map(names, name => createUser(name));

  const polls = await bluebird.mapSeries(new Array(POLLS_AMOUNT), async () => {
    const user = _.sample(users);
    return createPoll(user?._id || '');
  });

  await bluebird.map(users, user => {
    return createFeedback(user?._id || '');
  });

  await bluebird.map(users, user => {
    const pollsToVote = _.sampleSize(polls, _.random(0, POLLS_AMOUNT));
    return bluebird.map(pollsToVote, poll => createVote(user?._id || '', poll?._id || ''));
  });
};

populate().finally(mongoose.disconnect);