· 4 years ago · May 17, 2021, 11:40 AM
1public async insertTestReads(req: express.Request, res: express.Response, next: express.Errback) {
2 try {
3 const chapters = await getManager()
4 .createQueryBuilder(Chapter, 'ch')
5 .innerJoin(Comic, 'c', 'c.id = ch.comicId')
6 .innerJoin(Serie, 's', 's.id = c.serieId')
7 .leftJoin(Chapter18, 'ch18', 'ch18.chapterId = ch.id AND ch18.lang = s.defaultLang')
8 .select('ch.id id, ch18.pagesCount pagesCount, s.defaultLang defaultLang')
9 .where('ch.deleted = 0 AND c.deleted = 0 AND s.deleted = 0')
10 .getRawMany()
11 console.log(chapters)
12
13 const number = req.body.usersNumber;
14 for (let i = 0; i < number; i++) {
15 const user = await new User()
16 user.email = 'test-' + uuidv4() + '@gmail.com'
17 user.password = uuidv4()
18 user.username = uuidv4()
19 const date = Util.getRandomDateBetween(new Date('1971-01-01'), new Date('2010-12-31'))
20 user.dob = date;
21 user.gender = Gender.MALE;
22 user.avatarFileName = '';
23 user.deviceLangCode = 'es';
24 const createdAt = Util.getRandomDateBetween(new Date('2020-01-01'), new Date('2021-05-10'))
25 user.createdAt = createdAt
26 const updatedAt = Util.getRandomDateBetween(createdAt, new Date('2021-05-10'))
27 user.updatedAt = updatedAt
28 user.followers = 0;
29 user.following = 0;
30 user.totalLikes = 0;
31 user.countryId = 199;
32 user.accountValidationState = 2;
33 user.recoverPasswordSecretKey = '';
34 user.privateLibrary = false;
35 user.realLangCode = 'es';
36 user.appleId = null;
37 user.googleId = null;
38 user.coins = 10000;
39 user.serieMailFrequency = SERIE_FREQUENCY.ONE_AT_DAY;
40 user.allowDirectMessages = true;
41 user.pushNotifNewFollowers = true;
42 user.pushNotifDirectMessages = true;
43 user.pushNotifRepliesComments = true;
44 user.pushNotifLikesComments = true;
45 user.pushNotifCommentsSeriesYouFollow = true;
46 user.firstTimeLogged = false;
47 user.pushNotifSocialNews = true;
48 user.biography = null;
49 user.website = null;
50 user.location = null;
51 user.status = UserStatus.REGISTERED;
52 user.editorial = null;
53 user.author = null;
54 user.type = UserType.TEST;
55
56 user.save().then(async (usr) => {
57 for (const chapter of chapters) {
58 const userChapterStatus = new UserChapterStatus()
59 userChapterStatus.user = usr
60 userChapterStatus.chapter = await Chapter.findOne(chapter.id)
61 userChapterStatus.readed = false
62 userChapterStatus.like = false
63 const startedDate = Util.getRandomDateBetween(user.createdAt, new Date('2021-05-10'))
64 userChapterStatus.startedDatetime = startedDate
65 userChapterStatus.currentPage = Math.floor(Math.random() * chapter.pagesCount) + 1
66 userChapterStatus.readedPages = userChapterStatus.currentPage
67 userChapterStatus.updatedAt = Util.getRandomDateBetween(startedDate, new Date('2021-05-10'))
68 userChapterStatus.save().then(async (uchs) => {
69 const files = await getManager()
70 .createQueryBuilder(ChapterFile18, 'chf18')
71 .select('chf18.id id, chf18.pageNumber pageNumber')
72 .where('chf18.chapterId = :chapterId AND chf18.conversionQuality = :quality AND chf18.lang = :lang', { chapterId: chapter.id, quality: ConversionQuality.MEDIUM, lang: chapter.defaultLang })
73 .getRawMany()
74
75 const previousDate: Date = null;
76
77 for (const file of files) {
78 const userPageStatus = new UserPageStatus()
79 userPageStatus.userChapterStatus = uchs
80 userPageStatus.page = file.pageNumber
81 userPageStatus.views = 1
82 const fromDate = previousDate ? moment(previousDate).add(30, 'seconds') : moment(uchs.startedDatetime)
83 const createdAtDate = Util.getRandomDateBetween(fromDate.toDate(), moment(fromDate).add(5, 'minutes').toDate());
84 userPageStatus.createdAt = createdAt
85 userPageStatus.updatedAt = createdAt
86 await userPageStatus.save()
87 }
88 })
89 }
90 })
91 }
92
93 return res.status(200).send({
94 message: 'Users inserted successfully'
95 })
96 } catch (err) {
97 next(err);
98 }
99 }