feat: add stock keeping units management
- Created migration to drop `type` column from `stock_keeping_units` table. - Added `Guild` model to Prisma schema. - Implemented `BusinessActivitiesQueryService` for querying business activities. - Developed `GoodsSharedService` for handling goods creation and updates. - Introduced DTOs for creating and updating stock keeping units. - Created controller and service for managing stock keeping units. - Implemented response DTOs for stock keeping units service. - Established module for stock keeping units management.
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import { UploadedFileTypes } from '@/common/enums/enums'
|
||||
import { GoodSelect } from '@/generated/prisma/models'
|
||||
import { UploaderService } from '@/modules/uploader/uploader.service'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
@@ -6,7 +8,10 @@ import { CreateGoodDto, UpdateGoodDto } from './dto/good.dto'
|
||||
|
||||
@Injectable()
|
||||
export class ConsumerBusinessActivityGoodsService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly uploaderService: UploaderService,
|
||||
) {}
|
||||
|
||||
defaultSelect: GoodSelect = {
|
||||
id: true,
|
||||
@@ -64,12 +69,25 @@ export class ConsumerBusinessActivityGoodsService {
|
||||
return ResponseMapper.single(good)
|
||||
}
|
||||
|
||||
async create(business_activity_id: string, data: CreateGoodDto) {
|
||||
async create(
|
||||
business_activity_id: string,
|
||||
data: CreateGoodDto,
|
||||
file: Express.Multer.File,
|
||||
) {
|
||||
const { category_id, sku_id, measure_unit_id, ...rest } = data
|
||||
let image_url = ''
|
||||
if (file) {
|
||||
const uploadedUrl = await this.uploaderService.uploadFile(
|
||||
file,
|
||||
UploadedFileTypes.GOOD,
|
||||
)
|
||||
image_url = uploadedUrl || ''
|
||||
}
|
||||
|
||||
const good = await this.prisma.good.create({
|
||||
data: {
|
||||
...rest,
|
||||
image_url,
|
||||
business_activity: {
|
||||
connect: {
|
||||
id: business_activity_id,
|
||||
@@ -100,39 +118,61 @@ export class ConsumerBusinessActivityGoodsService {
|
||||
business_activity_id: string,
|
||||
id: string,
|
||||
data: UpdateGoodDto,
|
||||
file: Express.Multer.File,
|
||||
) {
|
||||
const { category_id, sku_id, measure_unit_id, ...rest } = data
|
||||
const good = await this.prisma.good.update({
|
||||
where: {
|
||||
business_activity: {
|
||||
id: business_activity_id,
|
||||
consumer_id,
|
||||
},
|
||||
id,
|
||||
},
|
||||
data: {
|
||||
business_activity: {
|
||||
connect: {
|
||||
const good = await this.prisma.$transaction(async tx => {
|
||||
const { category_id, sku_id, measure_unit_id, ...rest } = data
|
||||
let image_url = ''
|
||||
if (file) {
|
||||
const uploadedUrl = await this.uploaderService.uploadFile(
|
||||
file,
|
||||
UploadedFileTypes.GOOD,
|
||||
)
|
||||
image_url = uploadedUrl || ''
|
||||
}
|
||||
|
||||
const prevImage = await tx.good.findUnique({
|
||||
where: { id },
|
||||
select: { image_url: true },
|
||||
})
|
||||
|
||||
if (image_url && prevImage?.image_url) {
|
||||
this.uploaderService.deleteFile(prevImage.image_url, UploadedFileTypes.GOOD)
|
||||
}
|
||||
|
||||
return await tx.good.update({
|
||||
where: {
|
||||
business_activity: {
|
||||
id: business_activity_id,
|
||||
consumer_id,
|
||||
},
|
||||
id,
|
||||
},
|
||||
category: {
|
||||
connect: {
|
||||
id: category_id,
|
||||
data: {
|
||||
image_url,
|
||||
business_activity: {
|
||||
connect: {
|
||||
id: business_activity_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
sku: {
|
||||
connect: {
|
||||
id: sku_id,
|
||||
category: {
|
||||
connect: {
|
||||
id: category_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
measure_unit: {
|
||||
connect: {
|
||||
id: measure_unit_id,
|
||||
sku: {
|
||||
connect: {
|
||||
id: sku_id,
|
||||
},
|
||||
},
|
||||
measure_unit: {
|
||||
connect: {
|
||||
id: measure_unit_id,
|
||||
},
|
||||
},
|
||||
...rest,
|
||||
},
|
||||
...rest,
|
||||
},
|
||||
})
|
||||
})
|
||||
return ResponseMapper.update(good)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user