Skip to content

Commit

Permalink
fix: seperate content price to model collection
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulhakim2902 committed Dec 8, 2022
1 parent 8c6c901 commit e382298
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 62 deletions.
18 changes: 17 additions & 1 deletion src/__tests__/helpers/database.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
UserOTPRepository,
IdentityRepository,
UnlockableContentRepository,
ContentPriceRepository,
} from '../../repositories';
import {
ActivityLogService,
Expand Down Expand Up @@ -103,8 +104,18 @@ export async function givenRepositories(testdb: any) {
async () => postRepository,
async () => userRepository,
);
const contentPriceRepository: ContentPriceRepository =
new ContentPriceRepository(
testdb,
async () => currencyRepository,
async () => unlockableContentRepository,
);
const unlockableContentRepository: UnlockableContentRepository =
new UnlockableContentRepository(testdb, async () => userRepository);
new UnlockableContentRepository(
testdb,
async () => userRepository,
async () => contentPriceRepository,
);
const postRepository: PostRepository = new PostRepository(
testdb,
async () => peopleRepository,
Expand Down Expand Up @@ -321,6 +332,7 @@ export async function givenRepositories(testdb: any) {
);

const transactionService = new TransactionService(
contentPriceRepository,
currencyRepository,
peopleRepository,
transactionRepository,
Expand Down Expand Up @@ -415,6 +427,7 @@ export async function givenRepositories(testdb: any) {
);

const userService = new UserService(
contentPriceRepository,
changeEmailRequestRepository,
experienceRepository,
identityRepository,
Expand Down Expand Up @@ -484,6 +497,7 @@ export async function givenRepositories(testdb: any) {
socialMediaService,
tagService,
unlockableContentRepository,
contentPriceRepository,
};
}

Expand Down Expand Up @@ -513,6 +527,7 @@ export async function givenEmptyDatabase(testdb: any) {
userCurrencyRepository,
serverRepository,
unlockableContentRepository,
contentPriceRepository,
} = await givenRepositories(testdb);

await tagRepository.deleteAll();
Expand All @@ -539,4 +554,5 @@ export async function givenEmptyDatabase(testdb: any) {
await userCurrencyRepository.deleteAll();
await serverRepository.deleteAll();
await unlockableContentRepository.deleteAll();
await contentPriceRepository.deleteAll();
}
8 changes: 4 additions & 4 deletions src/controllers/user/unlockable-content.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
response,
} from '@loopback/rest';
import {PaginationInterceptor} from '../../interceptors';
import {UnlockableContent} from '../../models';
import {UnlockableContent, UnlockableContentWithPrice} from '../../models';
import {UserService} from '../../services';

@authenticate('jwt')
Expand All @@ -30,21 +30,21 @@ export class UserUnlockableContentController {
@response(200, {
description: 'CREATE user unlockable-content',
'application/json': {
schema: getModelSchemaRef(UnlockableContent),
schema: getModelSchemaRef(UnlockableContentWithPrice),
},
})
async create(
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(UnlockableContent, {
schema: getModelSchemaRef(UnlockableContentWithPrice, {
title: 'NewUnlockableContent',
exclude: ['id'],
}),
},
},
})
content: Omit<UnlockableContent, 'id'>,
content: Omit<UnlockableContentWithPrice, 'id'>,
): Promise<UnlockableContent> {
return this.userService.createUnlockableContent(content);
}
Expand Down
45 changes: 45 additions & 0 deletions src/models/content-price.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {Entity, model, property, belongsTo} from '@loopback/repository';
import {Currency} from './currency.model';
import {UnlockableContent} from './unlockable-content.model';

@model({
settings: {
strictObjectIDCoercion: true,
mongodb: {
collection: 'contentPrices',
},
},
})
export class ContentPrice extends Entity {
@property({
type: 'string',
id: true,
generated: true,
mongodb: {
dataType: 'ObjectId',
},
})
id: string;

@property({
type: 'number',
required: true,
})
amount: number;

@belongsTo(() => Currency)
currencyId: string;

@belongsTo(() => UnlockableContent)
unlockableContentId: string;

constructor(data?: Partial<ContentPrice>) {
super(data);
}
}

export interface ContentPriceRelations {
// describe navigational properties here
}

export type ContentPriceWithRelations = ContentPrice & ContentPriceRelations;
1 change: 1 addition & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ export * from './user-personal-access-token.model';
export * from './user.model';
export * from './vote.model';
export * from './wallet.model';
export * from './content-price.model';
31 changes: 19 additions & 12 deletions src/models/unlockable-content.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ import {
model,
property,
belongsTo,
hasMany,
} from '@loopback/repository';
import {Currency} from './currency.model';
import {User, UserWithRelations} from './user.model';

type Price = Currency & {
amount: string;
};
import {ContentPrice} from './content-price.model';

@model({
settings: {
Expand Down Expand Up @@ -46,13 +43,6 @@ export class UnlockableContent extends Entity {
})
content?: AnyObject;

@property({
type: 'array',
itemType: 'object',
required: true,
})
prices: Price[];

@property({
type: 'date',
required: false,
Expand All @@ -73,6 +63,9 @@ export class UnlockableContent extends Entity {
})
deletedAt?: string;

@hasMany(() => ContentPrice)
prices: ContentPrice[];

@belongsTo(() => User, {name: 'user'})
createdBy: string;

Expand All @@ -88,3 +81,17 @@ export interface UnlockableContentRelations {

export type UnlockableContentWithRelations = UnlockableContent &
UnlockableContentRelations;

export interface Price {
currencyId: string;
amount: number;
}

export class UnlockableContentWithPrice extends UnlockableContent {
@property({
type: 'array',
itemType: 'object',
required: false,
})
contentPrices: Price[];
}
54 changes: 54 additions & 0 deletions src/repositories/content-price.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {inject, Getter} from '@loopback/core';
import {
DefaultCrudRepository,
repository,
BelongsToAccessor,
} from '@loopback/repository';
import {MongoDataSource} from '../datasources';
import {
ContentPrice,
ContentPriceRelations,
Currency,
UnlockableContent,
} from '../models';
import {CurrencyRepository} from './currency.repository';
import {UnlockableContentRepository} from './unlockable-content.repository';

export class ContentPriceRepository extends DefaultCrudRepository<
ContentPrice,
typeof ContentPrice.prototype.id,
ContentPriceRelations
> {
public readonly currency: BelongsToAccessor<
Currency,
typeof ContentPrice.prototype.id
>;

public readonly unlockableContent: BelongsToAccessor<
UnlockableContent,
typeof ContentPrice.prototype.id
>;

constructor(
@inject('datasources.mongo') dataSource: MongoDataSource,
@repository.getter('CurrencyRepository')
protected currencyRepositoryGetter: Getter<CurrencyRepository>,
@repository.getter('UnlockableContentRepository')
protected unlockableContentRepositoryGetter: Getter<UnlockableContentRepository>,
) {
super(ContentPrice, dataSource);
this.unlockableContent = this.createBelongsToAccessorFor(
'unlockableContent',
unlockableContentRepositoryGetter,
);
this.registerInclusionResolver(
'unlockableContent',
this.unlockableContent.inclusionResolver,
);
this.currency = this.createBelongsToAccessorFor(
'currency',
currencyRepositoryGetter,
);
this.registerInclusionResolver('currency', this.currency.inclusionResolver);
}
}
1 change: 1 addition & 0 deletions src/repositories/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ export * from './user-otp.repository';
export * from './user-personal-access-token.repository';
export * from './change-email-request.repository';
export * from './request-create-new-user-by-email.repository';
export * from './content-price.repository';
21 changes: 20 additions & 1 deletion src/repositories/unlockable-content.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ import {
DefaultCrudRepository,
repository,
BelongsToAccessor,
HasManyRepositoryFactory,
} from '@loopback/repository';
import {MongoDataSource} from '../datasources';
import {UnlockableContent, UnlockableContentRelations, User} from '../models';
import {
UnlockableContent,
UnlockableContentRelations,
User,
ContentPrice,
} from '../models';
import {UserRepository} from './user.repository';
import {ContentPriceRepository} from './content-price.repository';

export class UnlockableContentRepository extends DefaultCrudRepository<
UnlockableContent,
Expand All @@ -18,12 +25,24 @@ export class UnlockableContentRepository extends DefaultCrudRepository<
typeof UnlockableContent.prototype.id
>;

public readonly prices: HasManyRepositoryFactory<
ContentPrice,
typeof UnlockableContent.prototype.id
>;

constructor(
@inject('datasources.mongo') dataSource: MongoDataSource,
@repository.getter('UserRepository')
protected userRepositoryGetter: Getter<UserRepository>,
@repository.getter('ContentPriceRepository')
protected contentPriceRepositoryGetter: Getter<ContentPriceRepository>,
) {
super(UnlockableContent, dataSource);
this.prices = this.createHasManyRepositoryFactoryFor(
'prices',
contentPriceRepositoryGetter,
);
this.registerInclusionResolver('prices', this.prices.inclusionResolver);
this.user = this.createBelongsToAccessorFor('user', userRepositoryGetter);
this.registerInclusionResolver('user', this.user.inclusionResolver);
}
Expand Down
48 changes: 24 additions & 24 deletions src/services/filter-builder.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,30 +203,6 @@ export class FilterBuilderService {
return this.finalizeFilter(filter, {userId, networkId});
}

public async userExperienceById(args: InvocationArgs): Promise<void> {
const filter = args[1] ?? {};
const include = [
{
relation: 'experience',
scope: {
include: [
{
relation: 'user',
scope: {
include: [{relation: 'accountSetting'}],
},
},
],
},
},
];

if (!filter.include) filter.include = include;
else filter.include.push(...include);

args[1] = filter;
}

public async userFriend(
filter: Filter<Friend>,
query: Query,
Expand Down Expand Up @@ -348,6 +324,30 @@ export class FilterBuilderService {
return this.finalizeFilter(filter, where);
}

public async userExperienceById(args: InvocationArgs): Promise<void> {
const filter = args[1] ?? {};
const include = [
{
relation: 'experience',
scope: {
include: [
{
relation: 'user',
scope: {
include: [{relation: 'accountSetting'}],
},
},
],
},
},
];

if (!filter.include) filter.include = include;
else filter.include.push(...include);

args[1] = filter;
}

public async userPostById(args: InvocationArgs): Promise<void> {
const filter = args[1] ?? {};
const experienceFilter = {
Expand Down
Loading

0 comments on commit e382298

Please sign in to comment.