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 87f14d0
Show file tree
Hide file tree
Showing 15 changed files with 255 additions and 77 deletions.
22 changes: 19 additions & 3 deletions src/__tests__/acceptance/unlockable-content.acceptance.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {Client, expect} from '@loopback/testlab';
import {omit} from 'lodash';
import {MyriadApiApplication} from '../../application';
import {ReferenceType} from '../../enums';
import {Token} from '../../interfaces';
import {Currency, UnlockableContentWithRelations, User} from '../../models';
import {
ContentPriceRepository,
CurrencyRepository,
ServerRepository,
TransactionRepository,
Expand All @@ -13,6 +15,7 @@ import {
import {
deleteAllRepository,
givenAccesToken,
givenContentPriceRepository,
givenCurrencyInstance,
givenCurrencyRepository,
givenOtherUser,
Expand All @@ -38,6 +41,7 @@ describe('UnlockableContentApplication', () => {
let transactionRepository: TransactionRepository;
let currencyRepository: CurrencyRepository;
let unlockableContentRepository: UnlockableContentRepository;
let contentPriceRepository: ContentPriceRepository;
let user: User;
let currency: Currency;

Expand All @@ -53,6 +57,7 @@ describe('UnlockableContentApplication', () => {
transactionRepository = await givenTransactionRepository(app);
unlockableContentRepository = await givenUnlockableContentRepository(app);
serverRepository = await givenServerRepository(app);
contentPriceRepository = await givenContentPriceRepository(app);
});

beforeEach(async () => {
Expand All @@ -72,15 +77,26 @@ describe('UnlockableContentApplication', () => {
});

it('create an unlockable content', async () => {
const content = givenUnlockableContent({createdBy: user.id});
const content = givenUnlockableContent({
createdBy: user.id,
contentPrices: [{currencyId: currency.id, amount: 100}],
});
const response = await client
.post('/user/unlockable-contents')
.set('Authorization', `Bearer ${token}`)
.send(content)
.expect(200);
expect(response.body).to.containDeep(content);
expect(response.body).to.containDeep(omit(content, ['contentPrices']));
const result = await unlockableContentRepository.findById(response.body.id);
expect(result).to.containDeep(content);
expect(result).to.containDeep(omit(content, ['contentPrices']));
const contentPrice = await contentPriceRepository.findOne({
where: {unlockableContentId: result.id},
});
expect(contentPrice).to.containEql({
currencyId: currency.id,
amount: 100,
unlockableContentId: result.id,
});
});

context('when dealing with a single persisted unlockable content', () => {
Expand Down
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();
}
20 changes: 8 additions & 12 deletions src/__tests__/helpers/given-instances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
SocialMediaVerificationDto,
Tag,
Transaction,
UnlockableContent,
UnlockableContentWithPrice,
User,
UserExperience,
UserReport,
Expand Down Expand Up @@ -892,28 +892,24 @@ export function givenIdentityInstance(
}

export function givenUnlockableContent(
unlockableContent?: Partial<UnlockableContent>,
unlockableContentWithPrice?: Partial<UnlockableContentWithPrice>,
) {
const data = Object.assign(
{
content: {
text: 'Hello world',
},
prices: [
{
...givenCurrency(),
amount: 100,
},
],
},
unlockableContent,
unlockableContentWithPrice,
);
return new UnlockableContent(data);
return new UnlockableContentWithPrice(data);
}

export function givenUnlockableContentInstance(
unlockableRepository: UnlockableContentRepository,
unlockableContent?: Partial<UnlockableContent>,
unlockableContentWithPrice?: Partial<UnlockableContentWithPrice>,
) {
return unlockableRepository.create(givenUnlockableContent(unlockableContent));
return unlockableRepository.create(
givenUnlockableContent(unlockableContentWithPrice),
);
}
7 changes: 7 additions & 0 deletions src/__tests__/helpers/given-repositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
ServerRepository,
IdentityRepository,
UnlockableContentRepository,
ContentPriceRepository,
} from '../../repositories';

export async function givenUserRepository(app: MyriadApiApplication) {
Expand Down Expand Up @@ -138,6 +139,10 @@ export async function givenUnlockableContentRepository(
return app.getRepository(UnlockableContentRepository);
}

export async function givenContentPriceRepository(app: MyriadApiApplication) {
return app.getRepository(ContentPriceRepository);
}

export async function deleteAllRepository(app: MyriadApiApplication) {
const userRepository = await givenUserRepository(app);
const friendRepository = await givenFriendRepository(app);
Expand Down Expand Up @@ -168,6 +173,7 @@ export async function deleteAllRepository(app: MyriadApiApplication) {
const unlockableContentRepository = await givenUnlockableContentRepository(
app,
);
const contentPriceRepository = await givenContentPriceRepository(app);

await userRepository.deleteAll();
await friendRepository.deleteAll();
Expand Down Expand Up @@ -196,4 +202,5 @@ export async function deleteAllRepository(app: MyriadApiApplication) {
await serverRepository.deleteAll();
await identityRepository.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';
35 changes: 23 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,21 @@ 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[];

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

0 comments on commit 87f14d0

Please sign in to comment.