1 | const mongoose = require('mongoose');
|
2 | const domain = require('../');
|
3 |
|
4 | async function main() {
|
5 | await mongoose.connect(process.env.MONGOLAB_URI);
|
6 |
|
7 | const applicationRepo = new domain.repository.Application(mongoose.connection);
|
8 | const memberRepo = new domain.repository.Member(mongoose.connection);
|
9 |
|
10 | const docs = await applicationRepo.applicationModel.find().exec();
|
11 | console.log(docs.length, 'applications found');
|
12 |
|
13 | let numCreated = 0;
|
14 | await Promise.all(docs.map(async (doc) => {
|
15 | const application = doc.toObject();
|
16 | const member = {
|
17 | project: { typeOf: application.project.typeOf, id: application.project.id },
|
18 | typeOf: 'OrganizationRole',
|
19 | member: {
|
20 | typeOf: domain.factory.creativeWorkType.WebApplication,
|
21 | id: application.id,
|
22 | name: application.name,
|
23 | hasRole: [{
|
24 | typeOf: 'OrganizationRole',
|
25 | roleName: 'customer',
|
26 | memberOf: { typeOf: application.project.typeOf, id: application.project.id }
|
27 | }]
|
28 | }
|
29 | };
|
30 | console.log(member);
|
31 |
|
32 | try {
|
33 | const doc = await memberRepo.memberModel.findOneAndUpdate(
|
34 | {
|
35 | 'project.id': member.project.id,
|
36 | 'member.id': member.member.id
|
37 | },
|
38 | member,
|
39 | { new: true, upsert: true }
|
40 | ).exec();
|
41 | console.log('created', doc);
|
42 | numCreated += 1;
|
43 |
|
44 | } catch (error) {
|
45 | console.error(error);
|
46 | }
|
47 |
|
48 | console.log('created.', numCreated);
|
49 | }))
|
50 | }
|
51 |
|
52 | main().then(() => {
|
53 | console.log('success!');
|
54 | }).catch((error) => {
|
55 | console.error(error);
|
56 | process.exit(1);
|
57 | });
|