-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add product favorite module & fix product video removal job
- Loading branch information
1 parent
6b9ce6b
commit 75b0dfc
Showing
9 changed files
with
364 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
apps/e-commerce/src/user/product-favorite/dto/get-product-favorite.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { IntersectionType } from '@nestjs/swagger'; | ||
import { ListFilter } from '@rahino/query-filter'; | ||
|
||
export class GetProductFavoriteDto extends IntersectionType(ListFilter) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './get-product-favorite.dto'; | ||
export * from './product-favorite.dto'; |
15 changes: 15 additions & 0 deletions
15
apps/e-commerce/src/user/product-favorite/dto/product-favorite.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { I18nTranslations } from 'apps/main/src/generated/i18n.generated'; | ||
import { AutoMap } from 'automapper-classes'; | ||
import { IsNumber } from 'class-validator'; | ||
import { i18nValidationMessage } from 'nestjs-i18n'; | ||
|
||
export class ProductFavoriteDto { | ||
@AutoMap() | ||
@IsNumber( | ||
{}, | ||
{ | ||
message: i18nValidationMessage<I18nTranslations>('validation.NUMBER'), | ||
}, | ||
) | ||
productId: bigint; | ||
} |
77 changes: 77 additions & 0 deletions
77
apps/e-commerce/src/user/product-favorite/product-favorite.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
HttpCode, | ||
HttpStatus, | ||
Param, | ||
Post, | ||
Put, | ||
Query, | ||
UseGuards, | ||
UseInterceptors, | ||
} from '@nestjs/common'; | ||
import { JsonResponseTransformInterceptor } from '@rahino/response/interceptor'; | ||
import { | ||
ApiBearerAuth, | ||
ApiOperation, | ||
ApiQuery, | ||
ApiTags, | ||
} from '@nestjs/swagger'; | ||
import { JwtGuard } from '@rahino/auth/guard'; | ||
import { ProductFavoriteService } from './product-favorite.service'; | ||
import { ProductFavoriteDto, GetProductFavoriteDto } from './dto'; | ||
import { GetUser } from '@rahino/auth/decorator'; | ||
import { User } from '@rahino/database/models/core/user.entity'; | ||
import { OptionalSessionGuard } from '../session/guard'; | ||
|
||
@ApiTags('ProductFavorite') | ||
@UseGuards(JwtGuard, OptionalSessionGuard) | ||
@ApiBearerAuth() | ||
@UseInterceptors(JsonResponseTransformInterceptor) | ||
@Controller({ | ||
path: '/api/ecommerce/user/productFavorites', | ||
version: ['1'], | ||
}) | ||
export class ProductFavoriteController { | ||
constructor(private service: ProductFavoriteService) {} | ||
|
||
// public url | ||
@ApiOperation({ description: 'show all my favorites' }) | ||
@Get('/') | ||
@ApiQuery({ | ||
name: 'filter', | ||
type: GetProductFavoriteDto, | ||
style: 'deepObject', | ||
explode: true, | ||
}) | ||
@HttpCode(HttpStatus.OK) | ||
async findAll(@GetUser() user: User, @Query() filter: GetProductFavoriteDto) { | ||
return await this.service.findAll(user, filter); | ||
} | ||
|
||
@ApiOperation({ description: 'show favorite by given id' }) | ||
@Get('/status/:productId') | ||
@HttpCode(HttpStatus.OK) | ||
async findById(@GetUser() user: User, @Param('productId') entityId: bigint) { | ||
return await this.service.statusByProductId(user, entityId); | ||
} | ||
|
||
@ApiOperation({ description: 'create favorite by user' }) | ||
@Post('/') | ||
@HttpCode(HttpStatus.CREATED) | ||
async create(@GetUser() user: User, @Body() dto: ProductFavoriteDto) { | ||
return await this.service.create(user, dto); | ||
} | ||
|
||
@ApiOperation({ description: 'remove favorite by user' }) | ||
@Delete('/product/:productId') | ||
@HttpCode(HttpStatus.OK) | ||
async deleteById( | ||
@GetUser() user: User, | ||
@Param('productId') entityId: bigint, | ||
) { | ||
return await this.service.deleteByProductId(user, entityId); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
apps/e-commerce/src/user/product-favorite/product-favorite.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ProductFavoriteController } from './product-favorite.controller'; | ||
import { ProductFavoriteService } from './product-favorite.service'; | ||
import { SequelizeModule } from '@nestjs/sequelize'; | ||
import { SessionModule } from '../session/session.module'; | ||
import { ProductModule } from '@rahino/ecommerce/product/product.module'; | ||
import { ECProductFavorite } from '@rahino/database/models/ecommerce-eav/ec-product-favorite'; | ||
|
||
@Module({ | ||
imports: [ | ||
SessionModule, | ||
ProductModule, | ||
SequelizeModule.forFeature([ECProductFavorite]), | ||
], | ||
controllers: [ProductFavoriteController], | ||
providers: [ProductFavoriteService], | ||
exports: [ProductFavoriteService], | ||
}) | ||
export class ProductFavoriteModule {} |
101 changes: 101 additions & 0 deletions
101
apps/e-commerce/src/user/product-favorite/product-favorite.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { | ||
BadRequestException, | ||
Injectable, | ||
NotFoundException, | ||
} from '@nestjs/common'; | ||
import { ProductFavoriteDto, GetProductFavoriteDto } from './dto'; | ||
import { InjectModel } from '@nestjs/sequelize'; | ||
import { QueryOptionsBuilder } from '@rahino/query-filter/sequelize-query-builder'; | ||
import { Op, Sequelize } from 'sequelize'; | ||
import * as _ from 'lodash'; | ||
import { ECAddress } from '@rahino/database/models/ecommerce-eav/ec-address.entity'; | ||
import { User } from '@rahino/database/models/core/user.entity'; | ||
import { ECProvince } from '@rahino/database/models/ecommerce-eav/ec-province.entity'; | ||
import { ECCity } from '@rahino/database/models/ecommerce-eav/ec-city.entity'; | ||
import { ECNeighborhood } from '@rahino/database/models/ecommerce-eav/ec-neighborhood.entity'; | ||
import { I18nTranslations } from 'apps/main/src/generated/i18n.generated'; | ||
import { I18nContext, I18nService } from 'nestjs-i18n'; | ||
import { ECProductFavorite } from '@rahino/database/models/ecommerce-eav/ec-product-favorite'; | ||
import { ProductRepositoryService } from '@rahino/ecommerce/product/service/product-repository.service'; | ||
|
||
@Injectable() | ||
export class ProductFavoriteService { | ||
constructor( | ||
@InjectModel(ECProductFavorite) | ||
private repository: typeof ECProductFavorite, | ||
private productRepositoryService: ProductRepositoryService, | ||
private readonly i18n: I18nService<I18nTranslations>, | ||
) {} | ||
|
||
async findAll(user: User, filter: GetProductFavoriteDto) { | ||
const queryBuilder = new QueryOptionsBuilder().filter({ | ||
userId: user.id, | ||
}); | ||
const count = await this.repository.count(queryBuilder.build()); | ||
|
||
let products = await this.repository.findAll(queryBuilder.build()); | ||
for (let index = 0; index < products.length; index++) { | ||
const product = products[index]; | ||
product.set( | ||
'product', | ||
await this.productRepositoryService.findById({}, product.id), | ||
); | ||
products[index] = product; | ||
} | ||
|
||
return { | ||
result: products, | ||
total: count, | ||
}; | ||
} | ||
|
||
async statusByProductId(user: User, entityId: bigint) { | ||
const item = await this.repository.findOne( | ||
new QueryOptionsBuilder() | ||
.filter({ | ||
userId: user.id, | ||
}) | ||
.filter({ productId: entityId }) | ||
.build(), | ||
); | ||
const result = item ? true : false; | ||
|
||
return { | ||
result: result, | ||
}; | ||
} | ||
|
||
async create(user: User, dto: ProductFavoriteDto) { | ||
const item = await this.repository.findOne( | ||
new QueryOptionsBuilder() | ||
.filter({ | ||
userId: user.id, | ||
}) | ||
.filter({ productId: dto.productId }) | ||
.build(), | ||
); | ||
if (!item) { | ||
await this.repository.create({ | ||
userId: user.id, | ||
productId: dto.productId, | ||
}); | ||
} | ||
|
||
return { | ||
result: 'ok', | ||
}; | ||
} | ||
|
||
async deleteByProductId(user: User, entityId: bigint) { | ||
await this.repository.destroy({ | ||
where: { | ||
userId: user.id, | ||
productId: entityId, | ||
}, | ||
}); | ||
|
||
return { | ||
result: 'ok', | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.