Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: exclusive content detail on comment #749

Merged
merged 2 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 113 additions & 4 deletions src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import {getFilePathFromSeedData, upload} from './utils/upload';
import fs, {existsSync} from 'fs';
import {FriendStatusType} from './enums';
import {UpdatePeopleProfileJob} from './jobs';
import {Asset} from './interfaces';

const jwt = require('jsonwebtoken');

Expand Down Expand Up @@ -238,10 +239,13 @@ export class MyriadApiApplication extends BootMixin(
}

async migrateSchema(options?: SchemaMigrationOptions): Promise<void> {
await super.migrateSchema(options);

if (!this.options?.skipMigrateSchema) await super.migrateSchema(options);
if (options?.existingSchema === 'drop') return this.databaseSeeding();
await Promise.allSettled([this.doMigrateUser()]);
await Promise.allSettled([
this.doMigrateComment(),
this.doMigratePost(),
this.doMigrateUser(),
]);
}

async databaseSeeding(): Promise<void> {
Expand Down Expand Up @@ -510,8 +514,12 @@ export class MyriadApiApplication extends BootMixin(
}

async doMigrateUser(): Promise<void> {
if (!this.doCollectionExists('user')) return;

const {userRepository} = await this.repositories();
const {count: totalUser} = await userRepository.count();
const {count: totalUser} = await userRepository.count({
email: {exists: true},
});
const bar = this.initializeProgressBar('Start Migrate User');
const promises = [];

Expand All @@ -520,6 +528,11 @@ export class MyriadApiApplication extends BootMixin(
const [user] = await userRepository.find({
limit: 1,
skip: i,
where: {
email: {
exists: true,
},
},
});

if (!user) continue;
Expand All @@ -536,6 +549,98 @@ export class MyriadApiApplication extends BootMixin(
bar.stop();
}

async doMigratePost(): Promise<void> {
if (!this.doCollectionExists('post')) return;

const {postRepository} = await this.repositories();
const {count: totalPost} = await postRepository.count(<AnyObject>{
'asset.exclusiveContents': {exists: true},
});
const bar = this.initializeProgressBar('Start Migrate Post');
const promises = [];

bar.start(totalPost - 1, 0);
for (let i = 0; i < totalPost; i++) {
const [post] = await postRepository.find(<AnyObject>{
limit: 1,
skip: i,
where: {
'asset.exclusiveContents': {
$exists: true,
},
},
});

if (!post) continue;
if (!post?.asset?.exclusiveContents) continue;
if (post.asset.exclusiveContents.length <= 0) continue;

const exclusiveContents = [];

for (const content of post.asset.exclusiveContents) {
const contentId = content.split('/').at(-1);
if (!contentId) continue;
exclusiveContents.push(contentId);
}

const asset: Asset = {...post.asset, exclusiveContents};

promises.push(postRepository.updateById(post.id, {asset}));

bar.update(i);
}

await Promise.allSettled(promises);

bar.stop();
}

async doMigrateComment(): Promise<void> {
if (!this.doCollectionExists('comment')) return;

const {commentRepository} = await this.repositories();
const {count: totalComments} = await commentRepository.count(<AnyObject>{
'asset.exclusiveContents': {exists: true},
});
const bar = this.initializeProgressBar('Start Migrate Post');
const promises = [];

bar.start(totalComments - 1, 0);
for (let i = 0; i < totalComments; i++) {
const [comment] = await commentRepository.find(<AnyObject>{
limit: 1,
skip: i,
where: {
'asset.exclusiveContents': {
$exists: true,
},
},
});

if (!comment) continue;
if (!comment?.asset?.exclusiveContents) continue;
if (comment.asset.exclusiveContents.length <= 0) continue;

const exclusiveContents = [];

for (const content of comment.asset.exclusiveContents) {
const contentId = content.split('/').at(-1);
if (!contentId) continue;
exclusiveContents.push(contentId);
}

const asset: Asset = {...comment.asset, exclusiveContents};

promises.push(commentRepository.updateById(comment.id, {asset}));

bar.update(i);
}

await Promise.allSettled(promises);

bar.stop();
}

async repositories(): Promise<Repositories> {
const accountSettingRepository = await this.getRepository(
AccountSettingRepository,
Expand Down Expand Up @@ -612,6 +717,10 @@ export class MyriadApiApplication extends BootMixin(
};
}

doCollectionExists(name: string) {
return this.options.alter.length > 0 && this.options.alter.includes(name);
}

initializeProgressBar(title: string) {
const cliProgress = require('cli-progress');
const colors = require('ansi-colors');
Expand Down
8 changes: 3 additions & 5 deletions src/interceptors/pagination.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,19 +371,17 @@ export class PaginationInterceptor implements Provider<Interceptor> {
const exclusiveContents = comment?.asset?.exclusiveContents ?? [];
if (exclusiveContents.length > 0) {
const updatedContents: AnyObject[] = [];
for (const url of exclusiveContents) {
const id = String(url).split('/').at(-1);
if (!id) continue;
for (const contentId of exclusiveContents) {
const prices = await this.contentPriceRepository.find({
include: ['currency'],
where: {
unlockableContentId: id,
unlockableContentId: contentId,
},
});
if (prices.length === 0) continue;
const updatedPrices = prices.map(price => {
return {
id,
id: contentId,
price: price.amount,
decimal: price?.currency?.decimal ?? 0,
symbol: price?.currency?.symbol ?? 'UNKNOWN',
Expand Down
12 changes: 10 additions & 2 deletions src/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@ import {MyriadApiApplication} from './application';

export async function migrate(args: string[]) {
const existingSchema = args.includes('--rebuild') ? 'drop' : 'alter';
const skipMigrateSchema = args.includes('--skip-migrate-schema')
? true
: false;
const envIndex = args.indexOf('--environment');
const environment = args.includes('--environment')
? args[envIndex + 1]
? args[envIndex + 1]
: 'development'
: undefined;
const alterIndex = args.indexOf('--alter');
const alter = args.includes('--alter') ? args[alterIndex + 1].split(',') : [];
const alter = args.includes('--alter')
? args[alterIndex + 1]?.split(',') ?? []
: [];
const dropIndex = args.indexOf('--drop');
const drop = args.includes('--drop') ? args[dropIndex + 1].split(',') : [];
const drop = args.includes('--drop')
? args[dropIndex + 1]?.split(',') ?? []
: [];
console.log('Migrating schemas (%s existing schema)', existingSchema);

const app = new MyriadApiApplication({
skipMigrateSchema,
environment,
alter,
drop,
Expand Down