2026-03-29 18:06:41 +03:30
|
|
|
import { GoodSelect } from '@/generated/prisma/models'
|
2026-02-12 20:31:04 +03:30
|
|
|
import { Injectable } from '@nestjs/common'
|
2026-03-07 11:25:11 +03:30
|
|
|
import { ResponseMapper } from '../../../common/response/response-mapper'
|
|
|
|
|
import { PrismaService } from '../../../prisma/prisma.service'
|
2026-02-12 20:31:04 +03:30
|
|
|
import { CreateGoodDto } from './dto/create-good.dto'
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class GoodsService {
|
|
|
|
|
constructor(private prisma: PrismaService) {}
|
|
|
|
|
|
2026-03-29 18:06:41 +03:30
|
|
|
private readonly defaultSelect: GoodSelect = {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
barcode: true,
|
|
|
|
|
created_at: true,
|
|
|
|
|
description: true,
|
|
|
|
|
sku: true,
|
|
|
|
|
local_sku: true,
|
|
|
|
|
is_default_guild_good: true,
|
|
|
|
|
pricing_model: true,
|
|
|
|
|
unit_type: true,
|
|
|
|
|
category: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
image_url: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findAll(complex_id: string, guild_id: string) {
|
2026-02-12 20:31:04 +03:30
|
|
|
const goods = await this.prisma.good.findMany({
|
|
|
|
|
where: {
|
2026-03-29 18:06:41 +03:30
|
|
|
OR: [
|
|
|
|
|
{
|
|
|
|
|
is_default_guild_good: true,
|
|
|
|
|
category: {
|
|
|
|
|
guild_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
complex_id,
|
|
|
|
|
},
|
|
|
|
|
],
|
2026-02-12 20:31:04 +03:30
|
|
|
},
|
2026-03-29 18:06:41 +03:30
|
|
|
select: this.defaultSelect,
|
2026-02-12 20:31:04 +03:30
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return ResponseMapper.list(goods)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 18:06:41 +03:30
|
|
|
async findOne(good_id: string, complex_id: string, guild_id: string) {
|
2026-02-12 20:31:04 +03:30
|
|
|
const good = await this.prisma.good.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
id: good_id,
|
2026-03-29 18:06:41 +03:30
|
|
|
complex: {
|
|
|
|
|
id: complex_id,
|
|
|
|
|
business_activity: {
|
|
|
|
|
guild_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-02-12 20:31:04 +03:30
|
|
|
},
|
2026-03-29 18:06:41 +03:30
|
|
|
select: this.defaultSelect,
|
2026-02-12 20:31:04 +03:30
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return ResponseMapper.single(good)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async create(data: CreateGoodDto, complex_id: string) {
|
|
|
|
|
const { category_id, ...rest } = data
|
|
|
|
|
|
|
|
|
|
const dataToCreate = {
|
|
|
|
|
...rest,
|
|
|
|
|
complex_id,
|
2026-03-07 11:25:11 +03:30
|
|
|
connect: {
|
|
|
|
|
category: {
|
2026-02-12 20:31:04 +03:30
|
|
|
id: category_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const good = await this.prisma.good.create({
|
2026-03-07 11:25:11 +03:30
|
|
|
data: {
|
|
|
|
|
...rest,
|
|
|
|
|
category: {
|
|
|
|
|
connect: {
|
|
|
|
|
id: category_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
complex: {
|
|
|
|
|
connect: {
|
|
|
|
|
id: complex_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-03-29 18:06:41 +03:30
|
|
|
select: this.defaultSelect,
|
2026-02-12 20:31:04 +03:30
|
|
|
})
|
2026-02-16 20:51:34 +03:30
|
|
|
|
2026-02-12 20:31:04 +03:30
|
|
|
return ResponseMapper.create(good)
|
|
|
|
|
}
|
|
|
|
|
}
|