transform core api codes into this project, update modules as admin/pos context modules
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
import { Controller } from '@nestjs/common'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
|
||||
@ApiTags('Admin')
|
||||
@Controller('admin')
|
||||
export class AdminController {}
|
||||
@@ -0,0 +1,14 @@
|
||||
// import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common'
|
||||
// import { Observable } from 'rxjs'
|
||||
|
||||
// @Injectable()
|
||||
// export class AdminGuard implements CanActivate {
|
||||
// canActivate(
|
||||
// _context: ExecutionContext,
|
||||
// ): boolean | Promise<boolean> | Observable<boolean> {
|
||||
// // Replace with real auth/permission checks (e.g., check JWT claims)
|
||||
// const request = _context.switchToHttp().getRequest()
|
||||
// const isAdminHeader = request.headers['x-admin']
|
||||
// return isAdminHeader === 'true' || request.isAdminRequest === true
|
||||
// }
|
||||
// }
|
||||
@@ -0,0 +1,24 @@
|
||||
import { Injectable, NestMiddleware, UnauthorizedException } from '@nestjs/common'
|
||||
import { NextFunction, Request, Response } from 'express'
|
||||
|
||||
@Injectable()
|
||||
export class AdminMiddleware implements NestMiddleware {
|
||||
use(req: Request, _res: Response, next: NextFunction) {
|
||||
console.log('first')
|
||||
// @ts-ignore
|
||||
console.log(req.dataPayload)
|
||||
// const a = reqTokenPayload()
|
||||
// console.log(a)
|
||||
|
||||
// Middleware-based admin check (replace with real auth logic)
|
||||
const isAdminHeader = req.headers['x-admin']
|
||||
|
||||
if (isAdminHeader === 'true') {
|
||||
// mark request if needed downstream
|
||||
req['isAdminRequest'] = true
|
||||
}
|
||||
return next()
|
||||
|
||||
throw new UnauthorizedException('Admin access required')
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import { MiddlewareConsumer, Module, NestModule, RequestMethod } from '@nestjs/common'
|
||||
import { AdminController } from './admin.controller'
|
||||
import { AdminMiddleware } from './admin.middleware'
|
||||
import { AdminDeviceBrandsModule } from './device-brands/device-brands.module'
|
||||
import { AdminDevicesModule } from './devices/devices.module'
|
||||
import { AdminGuildsModule } from './guilds/guilds.module'
|
||||
import { AdminLicensesModule } from './licenses/licenses.module'
|
||||
import { AdminPartnersModule } from './partners/partners.module'
|
||||
import { AdminProvidersModule } from './providers/providers.module'
|
||||
import { AdminTranslateModule } from './translate/translate.module'
|
||||
import { AdminUserAccountsModule } from './users/accounts/accounts.module'
|
||||
import { AdminUsersModule } from './users/users.module'
|
||||
|
||||
@Module({
|
||||
controllers: [AdminController],
|
||||
imports: [
|
||||
AdminUsersModule,
|
||||
AdminUserAccountsModule,
|
||||
AdminTranslateModule,
|
||||
AdminProvidersModule,
|
||||
AdminPartnersModule,
|
||||
AdminPartnersModule,
|
||||
AdminGuildsModule,
|
||||
AdminDeviceBrandsModule,
|
||||
AdminDevicesModule,
|
||||
AdminLicensesModule,
|
||||
],
|
||||
})
|
||||
export class AdminModule implements NestModule {
|
||||
configure(consumer: MiddlewareConsumer) {
|
||||
// apply middleware to all routes within this module (scoped under /admin)
|
||||
consumer.apply(AdminMiddleware).forRoutes({
|
||||
path: '/admin*path',
|
||||
method: RequestMethod.ALL,
|
||||
version: '1',
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { Body, Controller, Delete, Get, Param, Post, Put } from '@nestjs/common'
|
||||
import { DeviceBrandsService } from './device-brands.service'
|
||||
import { CreateDeviceBrandDto } from './dto/create-device-brand.dto'
|
||||
import { UpdateDeviceBrandDto } from './dto/update-device-brand.dto'
|
||||
|
||||
@Controller('admin/device_brands')
|
||||
export class DeviceBrandsController {
|
||||
constructor(private readonly deviceBrandsService: DeviceBrandsService) {}
|
||||
|
||||
@Get()
|
||||
async findAll() {
|
||||
return this.deviceBrandsService.findAll()
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async findOne(@Param('id') id: string) {
|
||||
return this.deviceBrandsService.findOne(id)
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(@Body() data: CreateDeviceBrandDto) {
|
||||
return this.deviceBrandsService.create(data)
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
async update(@Param('id') id: string, @Body() data: UpdateDeviceBrandDto) {
|
||||
return this.deviceBrandsService.update(id, data)
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
async delete(@Param('id') id: string) {
|
||||
return this.deviceBrandsService.delete(id)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { PrismaModule } from '@/prisma/prisma.module'
|
||||
import { Module } from '@nestjs/common'
|
||||
import { DeviceBrandsController } from './device-brands.controller'
|
||||
import { DeviceBrandsService } from './device-brands.service'
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [DeviceBrandsController],
|
||||
providers: [DeviceBrandsService],
|
||||
})
|
||||
export class AdminDeviceBrandsModule {}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
|
||||
@Injectable()
|
||||
export class DeviceBrandsService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async findAll() {
|
||||
const brands = await this.prisma.deviceBrand.findMany()
|
||||
return ResponseMapper.list(brands)
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const brand = await this.prisma.deviceBrand.findUnique({
|
||||
where: { id },
|
||||
})
|
||||
return ResponseMapper.single(brand)
|
||||
}
|
||||
|
||||
async create(data: any) {
|
||||
const device = await this.prisma.deviceBrand.create({ data })
|
||||
return ResponseMapper.create(device)
|
||||
}
|
||||
|
||||
async update(id: string, data: any) {
|
||||
const device = await this.prisma.deviceBrand.update({ where: { id }, data })
|
||||
return ResponseMapper.update(device)
|
||||
}
|
||||
|
||||
async delete(id: string) {
|
||||
await this.prisma.deviceBrand.delete({ where: { id } })
|
||||
return ResponseMapper.delete()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { IsString } from 'class-validator'
|
||||
|
||||
export class CreateDeviceBrandDto {
|
||||
@IsString()
|
||||
name: string
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/swagger'
|
||||
import { CreateDeviceBrandDto } from './create-device-brand.dto'
|
||||
|
||||
export class UpdateDeviceBrandDto extends PartialType(CreateDeviceBrandDto) {}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { Body, Controller, Delete, Get, Param, Post, Put } from '@nestjs/common'
|
||||
import { DevicesService } from './devices.service'
|
||||
import { CreateDeviceDto, UpdateDeviceDto } from './dto/device.dto'
|
||||
|
||||
@Controller('admin/devices')
|
||||
export class DevicesController {
|
||||
constructor(private readonly devicesService: DevicesService) {}
|
||||
|
||||
@Get()
|
||||
async findAll() {
|
||||
return this.devicesService.findAll()
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async findOne(@Param('id') id: string) {
|
||||
return this.devicesService.findOne(id)
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(@Body() data: CreateDeviceDto) {
|
||||
return this.devicesService.create(data)
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
async update(@Param('id') id: string, @Body() data: UpdateDeviceDto) {
|
||||
return this.devicesService.update(id, data)
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
async delete(@Param('id') id: string) {
|
||||
return this.devicesService.delete(id)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { PrismaModule } from '@/prisma/prisma.module'
|
||||
import { Module } from '@nestjs/common'
|
||||
import { DevicesController } from './devices.controller'
|
||||
import { DevicesService } from './devices.service'
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [DevicesController],
|
||||
providers: [DevicesService],
|
||||
})
|
||||
export class AdminDevicesModule {}
|
||||
@@ -0,0 +1,52 @@
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreateDeviceDto, UpdateDeviceDto } from './dto/device.dto'
|
||||
|
||||
@Injectable()
|
||||
export class DevicesService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async findAll(page = 1, pageSize = 10) {
|
||||
const [items, count] = await this.prisma.$transaction([
|
||||
this.prisma.device.findMany({
|
||||
skip: (page - 1) * pageSize,
|
||||
take: pageSize,
|
||||
include: { brand: true },
|
||||
}),
|
||||
this.prisma.device.count(),
|
||||
])
|
||||
return ResponseMapper.paginate(items, { count, page, pageSize })
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const item = await this.prisma.device.findUnique({
|
||||
where: { id },
|
||||
include: { brand: true },
|
||||
})
|
||||
return ResponseMapper.single(item)
|
||||
}
|
||||
|
||||
async create(data: CreateDeviceDto) {
|
||||
const { brand_id, ...rest } = data
|
||||
|
||||
const dataToCreate = {
|
||||
...rest,
|
||||
brand: { connect: { id: brand_id } },
|
||||
}
|
||||
const device = await this.prisma.device.create({
|
||||
data: dataToCreate,
|
||||
})
|
||||
return ResponseMapper.create(device)
|
||||
}
|
||||
|
||||
async update(id: string, data: UpdateDeviceDto) {
|
||||
const device = await this.prisma.device.update({ where: { id }, data })
|
||||
return ResponseMapper.update(device)
|
||||
}
|
||||
|
||||
async delete(id: string) {
|
||||
await this.prisma.device.delete({ where: { id } })
|
||||
return ResponseMapper.delete()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { IsOptional, IsString } from 'class-validator'
|
||||
|
||||
export class CreateDeviceDto {
|
||||
@IsString()
|
||||
name: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
os_version?: string
|
||||
|
||||
@IsString()
|
||||
brand_id: string
|
||||
}
|
||||
|
||||
export class UpdateDeviceDto {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
name?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
os_version?: string
|
||||
|
||||
@IsOptional()
|
||||
brand_id?: string
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { IsOptional, IsString } from 'class-validator'
|
||||
|
||||
export class CreateGuildDto {
|
||||
@IsString()
|
||||
name: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
code?: string
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/swagger'
|
||||
import { CreateGuildDto } from './create-guild.dto'
|
||||
|
||||
export class UpdateGuildDto extends PartialType(CreateGuildDto) {}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { Body, Controller, Get, Param, Patch, Post } from '@nestjs/common'
|
||||
import {
|
||||
CreateGoodCategoryDto,
|
||||
UpdateGoodCategoryDto,
|
||||
} from './dto/create-good-category.dto'
|
||||
import { GoodCategoriesService } from './good-categories.service'
|
||||
|
||||
@Controller('admin/guilds/:guildId/good_categories')
|
||||
export class GoodCategoriesController {
|
||||
constructor(private readonly goodCategoriesService: GoodCategoriesService) {}
|
||||
|
||||
@Get()
|
||||
findAll(@Param('guildId') guildId: string) {
|
||||
return this.goodCategoriesService.findAll(guildId)
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('guildId') guildId: string, @Param('id') id: string) {
|
||||
return this.goodCategoriesService.findOne(guildId, id)
|
||||
}
|
||||
|
||||
@Post()
|
||||
create(@Param('guildId') guildId: string, @Body() data: CreateGoodCategoryDto) {
|
||||
return this.goodCategoriesService.create(guildId, data)
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(
|
||||
@Param('guildId') guildId: string,
|
||||
@Param('id') id: string,
|
||||
@Body() data: UpdateGoodCategoryDto,
|
||||
) {
|
||||
return this.goodCategoriesService.update(guildId, id, data)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common'
|
||||
import { GoodCategoriesController } from './good-categories.controller'
|
||||
import { GoodCategoriesService } from './good-categories.service'
|
||||
|
||||
@Module({
|
||||
controllers: [GoodCategoriesController],
|
||||
providers: [GoodCategoriesService],
|
||||
})
|
||||
export class GuildGoodCategoriesModule {}
|
||||
@@ -0,0 +1,121 @@
|
||||
import { GoodCategoryOmit } from '@/generated/prisma/models'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import {
|
||||
CreateGoodCategoryDto,
|
||||
UpdateGoodCategoryDto,
|
||||
} from './dto/create-good-category.dto'
|
||||
|
||||
@Injectable()
|
||||
export class GoodCategoriesService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
private readonly goodCategoriesOmit = {
|
||||
complex_id: true,
|
||||
guild_id: true,
|
||||
is_default_guild_good: true,
|
||||
} as GoodCategoryOmit
|
||||
|
||||
async findAll(guild_id: string) {
|
||||
const categories = await this.prisma.goodCategory.findMany({
|
||||
where: {
|
||||
guild_id,
|
||||
is_default_guild_good: true,
|
||||
},
|
||||
include: {
|
||||
_count: {
|
||||
select: {
|
||||
goods: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
omit: this.goodCategoriesOmit,
|
||||
})
|
||||
|
||||
return ResponseMapper.list(
|
||||
categories.map(category => {
|
||||
const { _count, ...rest } = category
|
||||
return {
|
||||
...rest,
|
||||
goods_count: Number(_count.goods),
|
||||
}
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
async findOne(guild_id: string, categoryId: string) {
|
||||
const category = await this.prisma.goodCategory.findUnique({
|
||||
where: {
|
||||
guild_id,
|
||||
id: categoryId,
|
||||
is_default_guild_good: true,
|
||||
},
|
||||
include: {
|
||||
_count: {
|
||||
select: {
|
||||
goods: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
omit: this.goodCategoriesOmit,
|
||||
})
|
||||
|
||||
return ResponseMapper.single({
|
||||
...category,
|
||||
goods_count: Number(category?._count.goods),
|
||||
})
|
||||
}
|
||||
|
||||
create(guild_id: string, data: CreateGoodCategoryDto) {
|
||||
const category = this.prisma.goodCategory.create({
|
||||
data: {
|
||||
guild: {
|
||||
connect: {
|
||||
id: guild_id,
|
||||
},
|
||||
},
|
||||
name: data.name,
|
||||
image_url: data.image_url,
|
||||
description: data.description,
|
||||
is_default_guild_good: true,
|
||||
},
|
||||
omit: this.goodCategoriesOmit,
|
||||
})
|
||||
|
||||
return ResponseMapper.create({ ...category, goods_count: 0 })
|
||||
}
|
||||
|
||||
async update(guild_id: string, id: string, data: UpdateGoodCategoryDto) {
|
||||
const category = await this.prisma.goodCategory.update({
|
||||
where: {
|
||||
guild_id,
|
||||
id,
|
||||
},
|
||||
data: {
|
||||
guild: {
|
||||
connect: {
|
||||
id: guild_id,
|
||||
},
|
||||
},
|
||||
name: data.name,
|
||||
image_url: data.image_url,
|
||||
description: data.description,
|
||||
is_default_guild_good: true,
|
||||
},
|
||||
include: {
|
||||
_count: {
|
||||
select: {
|
||||
goods: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
omit: this.goodCategoriesOmit,
|
||||
})
|
||||
|
||||
return ResponseMapper.update({
|
||||
...category,
|
||||
goods_count: Number(category?._count.goods),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { ApiProperty, PartialType } from '@nestjs/swagger'
|
||||
import { IsNumber, IsOptional, IsString } from 'class-validator'
|
||||
|
||||
export class CreateGoodDto {
|
||||
@IsString()
|
||||
@ApiProperty()
|
||||
name: string
|
||||
|
||||
@IsString()
|
||||
@ApiProperty()
|
||||
sku: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@ApiProperty({ required: true })
|
||||
category_id: string
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@ApiProperty({ required: false })
|
||||
local_sku?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@ApiProperty({ required: false })
|
||||
barcode?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@ApiProperty({ required: false })
|
||||
description?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsNumber()
|
||||
@ApiProperty({ required: false })
|
||||
base_sale_price?: number
|
||||
}
|
||||
|
||||
export class UpdateGoodDto extends PartialType(CreateGoodDto) {}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Body, Controller, Get, Param, Post } from '@nestjs/common'
|
||||
import { CreateGoodDto } from './dto/create-good.dto'
|
||||
import { GoodsService } from './goods.service'
|
||||
|
||||
@Controller('admin/guilds/:guildId/goods')
|
||||
export class GoodsController {
|
||||
constructor(private readonly goodsService: GoodsService) {}
|
||||
|
||||
@Get()
|
||||
findAll(@Param('guildId') guildId: string) {
|
||||
return this.goodsService.findAll(guildId)
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('guildId') guildId: string, @Param('id') goodId: string) {
|
||||
return this.goodsService.findOne(guildId, goodId)
|
||||
}
|
||||
|
||||
@Post()
|
||||
create(@Param('guildId') guildId: string, @Body() data: CreateGoodDto) {
|
||||
return this.goodsService.create(guildId, data)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common'
|
||||
import { GoodsController } from './goods.controller'
|
||||
import { GoodsService } from './goods.service'
|
||||
|
||||
@Module({
|
||||
controllers: [GoodsController],
|
||||
providers: [GoodsService],
|
||||
})
|
||||
export class GuildGoodsModule {}
|
||||
@@ -0,0 +1,73 @@
|
||||
import { GoodOmit } from '@/generated/prisma/models'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreateGoodDto } from './dto/create-good.dto'
|
||||
|
||||
@Injectable()
|
||||
export class GoodsService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
private readonly goodOmit = {
|
||||
complex_id: true,
|
||||
barcode: true,
|
||||
base_sale_price: true,
|
||||
is_default_guild_good: true,
|
||||
local_sku: true,
|
||||
guild_id: true,
|
||||
} as GoodOmit
|
||||
|
||||
async findAll(guildId: string) {
|
||||
const [goods, count] = await this.prisma.$transaction([
|
||||
this.prisma.good.findMany({
|
||||
where: {
|
||||
guild_id: guildId,
|
||||
is_default_guild_good: true,
|
||||
},
|
||||
omit: this.goodOmit,
|
||||
}),
|
||||
this.prisma.good.count(),
|
||||
])
|
||||
return ResponseMapper.paginate(goods, {
|
||||
count,
|
||||
})
|
||||
}
|
||||
|
||||
async findOne(guild_id: string, id: string) {
|
||||
const good = await this.prisma.good.findUnique({
|
||||
where: {
|
||||
guild_id,
|
||||
id,
|
||||
is_default_guild_good: true,
|
||||
},
|
||||
|
||||
omit: this.goodOmit,
|
||||
})
|
||||
|
||||
return ResponseMapper.single(good)
|
||||
}
|
||||
|
||||
async create(guild_id: string, data: CreateGoodDto) {
|
||||
const { category_id, ...rest } = data
|
||||
|
||||
const good = await this.prisma.good.create({
|
||||
data: {
|
||||
guild: {
|
||||
connect: {
|
||||
id: guild_id,
|
||||
},
|
||||
},
|
||||
category: {
|
||||
connect: {
|
||||
id: category_id,
|
||||
},
|
||||
},
|
||||
...rest,
|
||||
is_default_guild_good: true,
|
||||
},
|
||||
omit: this.goodOmit,
|
||||
})
|
||||
|
||||
return ResponseMapper.create(good)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { Body, Controller, Get, Param, Post, Put } from '@nestjs/common'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
import { CreateGuildDto } from './dto/create-guild.dto'
|
||||
import { UpdateGuildDto } from './dto/update-guild.dto'
|
||||
import { GuildsService } from './guilds.service'
|
||||
|
||||
@ApiTags('guilds')
|
||||
@Controller('admin/guilds')
|
||||
export class GuildsController {
|
||||
constructor(private readonly guildsService: GuildsService) {}
|
||||
|
||||
@Get()
|
||||
async findAll() {
|
||||
return this.guildsService.findAll()
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async findOne(@Param('id') id: string) {
|
||||
return this.guildsService.findOne(id)
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(@Body() data: CreateGuildDto) {
|
||||
return await this.guildsService.create(data)
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
async update(@Param('id') id: string, @Body() data: UpdateGuildDto) {
|
||||
return await this.guildsService.update(id, data)
|
||||
}
|
||||
|
||||
// @Delete(':id')
|
||||
// async delete(@Param('id') id: string) {
|
||||
// await this.guildsService.delete(id)
|
||||
// return ResponseMapper.delete()
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { PrismaModule } from '@/prisma/prisma.module'
|
||||
import { Module } from '@nestjs/common'
|
||||
import { GuildGoodCategoriesModule } from './good-categories/good-categories.module'
|
||||
import { GuildGoodsModule } from './goods/goods.module'
|
||||
import { GuildsController } from './guilds.controller'
|
||||
import { GuildsService } from './guilds.service'
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule, GuildGoodsModule, GuildGoodCategoriesModule],
|
||||
providers: [GuildsService],
|
||||
controllers: [GuildsController],
|
||||
})
|
||||
export class AdminGuildsModule {}
|
||||
@@ -0,0 +1,66 @@
|
||||
import { ResponseMapper } from '@/common/response/response-mapper'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { CreateGuildDto } from './dto/create-guild.dto'
|
||||
import { UpdateGuildDto } from './dto/update-guild.dto'
|
||||
|
||||
@Injectable()
|
||||
export class GuildsService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
async findAll() {
|
||||
const [guilds, count] = await this.prisma.$transaction([
|
||||
this.prisma.guild.findMany({
|
||||
include: {
|
||||
_count: {
|
||||
select: {
|
||||
business_activities: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
this.prisma.guild.count(),
|
||||
])
|
||||
|
||||
return ResponseMapper.paginate(
|
||||
guilds.map(guild => {
|
||||
const { _count, ...rest } = guild
|
||||
return {
|
||||
...rest,
|
||||
businessActivitiesCount: guild._count.business_activities,
|
||||
}
|
||||
}),
|
||||
{ count },
|
||||
)
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const guild = await this.prisma.guild.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
business_activities: {
|
||||
omit: {
|
||||
guild_id: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
return ResponseMapper.single(guild)
|
||||
}
|
||||
|
||||
async create(data: CreateGuildDto) {
|
||||
const guild = await this.prisma.guild.create({ data })
|
||||
return ResponseMapper.create(guild)
|
||||
}
|
||||
|
||||
async update(id: string, data: UpdateGuildDto) {
|
||||
const guild = await this.prisma.guild.update({ where: { id }, data })
|
||||
return ResponseMapper.update(guild)
|
||||
}
|
||||
|
||||
// async delete(id: string) {
|
||||
// await this.prisma.guild.delete({ where: { id } })
|
||||
// return ResponseMapper.
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import { ApiProperty } from '@nestjs/swagger'
|
||||
import { IsDateString, IsEnum, IsOptional, IsString } from 'class-validator'
|
||||
import { LicenseStatus } from 'generated/prisma/enums'
|
||||
|
||||
export class CreateLicenseDto {
|
||||
@IsString()
|
||||
@ApiProperty()
|
||||
account_id: string
|
||||
|
||||
@IsString()
|
||||
@ApiProperty()
|
||||
pos_id: string
|
||||
|
||||
@IsString()
|
||||
@ApiProperty({ required: true })
|
||||
partner_id: string
|
||||
|
||||
@IsDateString()
|
||||
@IsOptional()
|
||||
@ApiProperty({ required: false })
|
||||
starts_at: string
|
||||
|
||||
@IsDateString()
|
||||
@IsOptional()
|
||||
@ApiProperty({ required: false })
|
||||
expires_at: string
|
||||
}
|
||||
|
||||
export class UpdateLicenseDto {
|
||||
@IsString()
|
||||
pos_id?: string
|
||||
|
||||
@IsDateString()
|
||||
starts_at?: string
|
||||
|
||||
@IsDateString()
|
||||
expires_at?: string
|
||||
|
||||
@IsEnum(LicenseStatus)
|
||||
status?: LicenseStatus
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { Body, Controller, Get, Param, Post, Put } from '@nestjs/common'
|
||||
import { CreateLicenseDto, UpdateLicenseDto } from './dto/license.dto'
|
||||
import { PartnerLicensesService } from './licenses.service'
|
||||
|
||||
@Controller('admin/licenses')
|
||||
export class LicensesController {
|
||||
constructor(private readonly licensesService: PartnerLicensesService) {}
|
||||
|
||||
@Get()
|
||||
async findAll() {
|
||||
return this.licensesService.findAll()
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async findOne(@Param('id') id: string) {
|
||||
return this.licensesService.findOne(id)
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(@Body() data: CreateLicenseDto) {
|
||||
return this.licensesService.create(data)
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
async update(@Param('id') id: string, @Body() data: UpdateLicenseDto) {
|
||||
return this.licensesService.update(id, data)
|
||||
}
|
||||
|
||||
// @Delete(':id')
|
||||
// async delete(@Param('id') id: string) {
|
||||
// return this.licensesService.delete(id)
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { PrismaModule } from '@/prisma/prisma.module'
|
||||
import { Module } from '@nestjs/common'
|
||||
import { LicensesController } from './licenses.controller'
|
||||
import { PartnerLicensesService } from './licenses.service'
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [LicensesController],
|
||||
providers: [PartnerLicensesService],
|
||||
exports: [PartnerLicensesService],
|
||||
})
|
||||
export class AdminLicensesModule {}
|
||||
@@ -0,0 +1,122 @@
|
||||
import { LicenseInclude } from '@/generated/prisma/models'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { LicenseStatus } from 'generated/prisma/enums'
|
||||
import { CreateLicenseDto, UpdateLicenseDto } from './dto/license.dto'
|
||||
|
||||
@Injectable()
|
||||
export class PartnerLicensesService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
private readonly licenseInclude = {
|
||||
partner: true,
|
||||
pos: {
|
||||
include: {
|
||||
complex: true,
|
||||
provider: true,
|
||||
accounts: {
|
||||
include: {
|
||||
user: true,
|
||||
},
|
||||
omit: {
|
||||
business_id: true,
|
||||
partner_id: true,
|
||||
provider_id: true,
|
||||
pos_id: true,
|
||||
},
|
||||
},
|
||||
device: {
|
||||
include: {
|
||||
brand: true,
|
||||
},
|
||||
omit: {
|
||||
brand_id: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as LicenseInclude
|
||||
|
||||
async findAll() {
|
||||
const [licenses, count] = await this.prisma.$transaction([
|
||||
this.prisma.license.findMany({
|
||||
include: this.licenseInclude,
|
||||
omit: {
|
||||
pos_id: true,
|
||||
partner_id: true,
|
||||
},
|
||||
}),
|
||||
this.prisma.license.count(),
|
||||
])
|
||||
return ResponseMapper.paginate(
|
||||
licenses.map(license => {
|
||||
const { pos, ...rest } = license
|
||||
// @ts-ignore
|
||||
const { accounts, ...posRest } = pos
|
||||
return {
|
||||
account: accounts[0],
|
||||
pos: posRest,
|
||||
...rest,
|
||||
}
|
||||
}),
|
||||
{
|
||||
count,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const license = await this.prisma.license.findFirst({
|
||||
where: { id },
|
||||
include: this.licenseInclude,
|
||||
omit: {
|
||||
pos_id: true,
|
||||
partner_id: true,
|
||||
},
|
||||
})
|
||||
if (license) {
|
||||
const { pos, ...rest } = license
|
||||
// @ts-ignore
|
||||
const { accounts, ...posRest } = pos
|
||||
return ResponseMapper.single({
|
||||
account: accounts[0],
|
||||
pos: posRest,
|
||||
...rest,
|
||||
})
|
||||
}
|
||||
|
||||
return ResponseMapper.single(license)
|
||||
}
|
||||
|
||||
async create(data: CreateLicenseDto) {
|
||||
const date = new Date(data.starts_at || '')
|
||||
const expiresAt =
|
||||
data.expires_at || new Date(date.setFullYear(date.getFullYear() + 1))
|
||||
|
||||
const license = await this.prisma.license.create({
|
||||
data: {
|
||||
partner_id: data.partner_id,
|
||||
pos_id: data.pos_id,
|
||||
starts_at: date,
|
||||
expires_at: expiresAt,
|
||||
status: LicenseStatus.ACTIVE,
|
||||
},
|
||||
})
|
||||
|
||||
return ResponseMapper.create(license)
|
||||
}
|
||||
|
||||
async update(id: string, data: UpdateLicenseDto) {
|
||||
const license = await this.prisma.license.update({
|
||||
where: { id },
|
||||
data: { ...data },
|
||||
})
|
||||
return ResponseMapper.update(license)
|
||||
}
|
||||
|
||||
async delete(id: string) {
|
||||
await this.prisma.license.delete({ where: { id } })
|
||||
return ResponseMapper.delete()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { IsOptional, IsString } from 'class-validator'
|
||||
|
||||
export class CreatePartnerDto {
|
||||
@IsString()
|
||||
name: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
code?: string
|
||||
}
|
||||
|
||||
export class UpdatePartnerDto {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
name?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
code?: string
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
import { IsDateString, IsEnum, IsOptional, IsString } from 'class-validator'
|
||||
import { AccountStatus, LicenseStatus, POSType } from 'generated/prisma/enums'
|
||||
|
||||
export class CreateLicenseDto {
|
||||
@IsString()
|
||||
first_name: string
|
||||
|
||||
@IsString()
|
||||
last_name: string
|
||||
|
||||
@IsString()
|
||||
mobile_number: string
|
||||
|
||||
@IsString()
|
||||
national_code: string
|
||||
|
||||
@IsString()
|
||||
username: string
|
||||
|
||||
@IsString()
|
||||
password: string
|
||||
|
||||
@IsEnum(AccountStatus)
|
||||
account_status: AccountStatus
|
||||
|
||||
@IsString()
|
||||
guild: string
|
||||
|
||||
@IsString()
|
||||
business_activity_name: string
|
||||
|
||||
@IsString()
|
||||
tax_id: string
|
||||
|
||||
@IsString()
|
||||
complex_name: string
|
||||
|
||||
@IsString()
|
||||
pos_serial: string
|
||||
|
||||
@IsString()
|
||||
pos_status: string
|
||||
|
||||
@IsEnum(POSType)
|
||||
pos_type: POSType
|
||||
|
||||
@IsString()
|
||||
pos_device: string
|
||||
|
||||
@IsDateString()
|
||||
@IsOptional()
|
||||
starts_at: string
|
||||
|
||||
@IsDateString()
|
||||
@IsOptional()
|
||||
expires_at: string
|
||||
}
|
||||
|
||||
export class UpdateLicenseDto {
|
||||
@IsString()
|
||||
pos_id?: string
|
||||
|
||||
@IsDateString()
|
||||
starts_at?: string
|
||||
|
||||
@IsDateString()
|
||||
expires_at?: string
|
||||
|
||||
@IsEnum(LicenseStatus)
|
||||
status?: LicenseStatus
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { Body, Controller, Delete, Get, Param, Post, Put } from '@nestjs/common'
|
||||
import { CreateLicenseDto, UpdateLicenseDto } from './dto/license.dto'
|
||||
import { PartnerLicensesService } from './licenses.service'
|
||||
|
||||
@Controller('admin/partners/:partnerId/licenses')
|
||||
export class PartnerLicensesController {
|
||||
constructor(private readonly licensesService: PartnerLicensesService) {}
|
||||
|
||||
@Get()
|
||||
async findAll(@Param('partnerId') partnerId: string) {
|
||||
return this.licensesService.findAll(partnerId)
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async findOne(@Param('partnerId') partnerId: string, @Param('id') id: string) {
|
||||
return this.licensesService.findOne(partnerId, id)
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(@Param('partnerId') partnerId: string, @Body() data: CreateLicenseDto) {
|
||||
return this.licensesService.create(partnerId, data)
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
async update(
|
||||
@Param('partnerId') partnerId: string,
|
||||
@Param('id') id: string,
|
||||
@Body() data: UpdateLicenseDto,
|
||||
) {
|
||||
return this.licensesService.update(partnerId, id, data)
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
async delete(@Param('partnerId') partnerId: string, @Param('id') id: string) {
|
||||
return this.licensesService.delete(partnerId, id)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { PrismaModule } from '@/prisma/prisma.module'
|
||||
import { Module } from '@nestjs/common'
|
||||
import { PartnerLicensesController } from './licenses.controller'
|
||||
import { PartnerLicensesService } from './licenses.service'
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [PartnerLicensesController],
|
||||
providers: [PartnerLicensesService],
|
||||
exports: [PartnerLicensesService],
|
||||
})
|
||||
export class AdminPartnerLicensesModule {}
|
||||
@@ -0,0 +1,197 @@
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreateLicenseDto, UpdateLicenseDto } from './dto/license.dto'
|
||||
|
||||
@Injectable()
|
||||
export class PartnerLicensesService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async findAll(partnerId: string) {
|
||||
const licenses = await this.prisma.license.findMany({
|
||||
where: { partner_id: partnerId },
|
||||
include: {
|
||||
pos: {
|
||||
include: {
|
||||
complex: true,
|
||||
provider: true,
|
||||
|
||||
accounts: {
|
||||
include: {
|
||||
user: true,
|
||||
},
|
||||
omit: {
|
||||
business_id: true,
|
||||
partner_id: true,
|
||||
provider_id: true,
|
||||
pos_id: true,
|
||||
},
|
||||
},
|
||||
device: {
|
||||
include: {
|
||||
brand: true,
|
||||
},
|
||||
omit: {
|
||||
brand_id: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
omit: {
|
||||
pos_id: true,
|
||||
partner_id: true,
|
||||
},
|
||||
})
|
||||
return ResponseMapper.list(
|
||||
licenses.map(license => {
|
||||
const { pos, ...rest } = license
|
||||
const { accounts, ...posRest } = pos
|
||||
return {
|
||||
account: accounts[0],
|
||||
pos: posRest,
|
||||
...rest,
|
||||
}
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
async findOne(partnerId: string, id: string) {
|
||||
const license = await this.prisma.license.findFirst({
|
||||
where: { id, partner_id: partnerId },
|
||||
include: {
|
||||
pos: {
|
||||
include: {
|
||||
complex: true,
|
||||
provider: true,
|
||||
|
||||
accounts: {
|
||||
include: {
|
||||
user: true,
|
||||
},
|
||||
omit: {
|
||||
business_id: true,
|
||||
partner_id: true,
|
||||
provider_id: true,
|
||||
pos_id: true,
|
||||
},
|
||||
},
|
||||
device: {
|
||||
include: {
|
||||
brand: true,
|
||||
},
|
||||
omit: {
|
||||
brand_id: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
omit: {
|
||||
pos_id: true,
|
||||
partner_id: true,
|
||||
},
|
||||
})
|
||||
if (license) {
|
||||
const { pos, ...rest } = license
|
||||
const { accounts, ...posRest } = pos
|
||||
return ResponseMapper.single({
|
||||
account: accounts[0],
|
||||
pos: posRest,
|
||||
...rest,
|
||||
})
|
||||
}
|
||||
|
||||
return ResponseMapper.single(license)
|
||||
}
|
||||
|
||||
async create(partnerId: string, data: CreateLicenseDto) {
|
||||
// const license = await this.prisma.$transaction(async tx => {
|
||||
// const user = await tx.user.upsert({
|
||||
// where: {
|
||||
// mobile_number: data.mobile_number,
|
||||
// },
|
||||
// update: {},
|
||||
// create: {
|
||||
// first_name: data.first_name,
|
||||
// last_name: data.last_name,
|
||||
// mobile_number: data.mobile_number,
|
||||
// national_code: data.national_code,
|
||||
// },
|
||||
// })
|
||||
|
||||
// const business = await tx.businessActivity.create({
|
||||
// data: {
|
||||
// name: data.business_activity_name,
|
||||
// guild_id: data.guild,
|
||||
// user: {
|
||||
// connect: {
|
||||
// id: user.id,
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// })
|
||||
|
||||
// const complex = await tx.complex.create({
|
||||
// data: {
|
||||
// name: data.complex_name,
|
||||
// tax_id: data.tax_id,
|
||||
// business_activity_id: business.id,
|
||||
// },
|
||||
// })
|
||||
|
||||
// const pos = await tx.pos.create({
|
||||
// data: {
|
||||
// pos_type: data.pos_type,
|
||||
// serial: data.pos_serial,
|
||||
// device_id: data.pos_device,
|
||||
// complex_id: complex.id,
|
||||
// },
|
||||
// })
|
||||
|
||||
// const date = new Date()
|
||||
// const expiresAt = new Date(date.setFullYear(date.getFullYear() + 1))
|
||||
|
||||
// const license = await tx.license.create({
|
||||
// data: {
|
||||
// partner_id: partnerId,
|
||||
// pos_id: pos.id,
|
||||
// starts_at: data.starts_at || new Date(),
|
||||
// expires_at: data.expires_at || expiresAt,
|
||||
// status: LicenseStatus.ACTIVE,
|
||||
// },
|
||||
// })
|
||||
|
||||
// const hashedPassword = await PasswordUtil.hash(data.password)
|
||||
|
||||
// const account = await tx.account.create({
|
||||
// data: {
|
||||
// username: data.username,
|
||||
// password: hashedPassword,
|
||||
// type: AccountType.POS,
|
||||
// user_id: user.id,
|
||||
// pos_id: pos.id,
|
||||
// status: data.account_status,
|
||||
// },
|
||||
// })
|
||||
|
||||
// return {
|
||||
// license,
|
||||
// }
|
||||
// })
|
||||
return ResponseMapper.create('license')
|
||||
}
|
||||
|
||||
async update(partnerId: string, id: string, data: UpdateLicenseDto) {
|
||||
const license = await this.prisma.license.update({
|
||||
where: { id },
|
||||
data: { ...data, partner_id: partnerId },
|
||||
})
|
||||
return ResponseMapper.update(license)
|
||||
}
|
||||
|
||||
async delete(partnerId: string, id: string) {
|
||||
await this.prisma.license.delete({ where: { id } })
|
||||
return ResponseMapper.delete()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { Body, Controller, Delete, Get, Param, Post, Put } from '@nestjs/common'
|
||||
import { AccountsService } from './accounts.service'
|
||||
import { CreateAccountDto, UpdateAccountDto } from './dto/account.dto'
|
||||
|
||||
@Controller('admin/partners/:partnerId/accounts')
|
||||
export class AccountsController {
|
||||
constructor(private readonly accountsService: AccountsService) {}
|
||||
|
||||
@Get()
|
||||
async findAll(@Param('partnerId') partnerId: string) {
|
||||
return this.accountsService.findAll(partnerId)
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async findOne(@Param('id') id: string) {
|
||||
return this.accountsService.findOne(id)
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(@Param('partnerId') partnerId: string, @Body() data: CreateAccountDto) {
|
||||
return this.accountsService.create(partnerId, data)
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
async update(@Param('id') id: string, @Body() data: UpdateAccountDto) {
|
||||
return this.accountsService.update(id, data)
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
async delete(@Param('id') id: string) {
|
||||
return this.accountsService.delete(id)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { PrismaModule } from '@/prisma/prisma.module'
|
||||
import { Module } from '@nestjs/common'
|
||||
import { AccountsController } from './accounts.controller'
|
||||
import { AccountsService } from './accounts.service'
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [AccountsController],
|
||||
providers: [AccountsService],
|
||||
exports: [AccountsService],
|
||||
})
|
||||
export class AdminPartnerAccountsModule {}
|
||||
@@ -0,0 +1,82 @@
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { PasswordUtil } from 'common/utils/password.util'
|
||||
import { AccountType } from 'generated/prisma/enums'
|
||||
import { CreateAccountDto, UpdateAccountDto } from './dto/account.dto'
|
||||
|
||||
@Injectable()
|
||||
export class AccountsService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async findAll(partnerId: string) {
|
||||
const accounts = await this.prisma.account.findMany({
|
||||
where: { partner_id: partnerId },
|
||||
omit: {
|
||||
password: true,
|
||||
user_id: true,
|
||||
partner_id: true,
|
||||
type: true,
|
||||
pos_id: true,
|
||||
provider_id: true,
|
||||
business_id: true,
|
||||
},
|
||||
})
|
||||
return ResponseMapper.list(accounts)
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const account = await this.prisma.account.findUnique({
|
||||
where: { id },
|
||||
omit: { password: true, user_id: true },
|
||||
include: {
|
||||
partner: true,
|
||||
user: true,
|
||||
},
|
||||
})
|
||||
return ResponseMapper.single(account)
|
||||
}
|
||||
|
||||
async create(partnerId: string, data: CreateAccountDto) {
|
||||
const { username, status, password, ...userData } = data
|
||||
|
||||
const result = await this.prisma.$transaction(async tx => {
|
||||
let user = await tx.user.upsert({
|
||||
where: {
|
||||
mobile_number: userData.mobile_number,
|
||||
},
|
||||
update: {},
|
||||
create: userData,
|
||||
})
|
||||
|
||||
const hashedPassword = await PasswordUtil.hash(password)
|
||||
|
||||
const dataToCreate = {
|
||||
username,
|
||||
status,
|
||||
password: hashedPassword,
|
||||
user_id: user.id,
|
||||
type: AccountType.PARTNER,
|
||||
partner_id: partnerId, // Connect account to partner
|
||||
}
|
||||
|
||||
const account = await tx.account.create({
|
||||
data: dataToCreate,
|
||||
})
|
||||
|
||||
return account
|
||||
})
|
||||
|
||||
return ResponseMapper.create(result)
|
||||
}
|
||||
|
||||
async update(id: string, data: UpdateAccountDto) {
|
||||
const account = await this.prisma.account.update({ where: { id }, data })
|
||||
return ResponseMapper.update(account)
|
||||
}
|
||||
|
||||
async delete(id: string) {
|
||||
await this.prisma.account.delete({ where: { id } })
|
||||
return ResponseMapper.delete()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import { IsEnum, IsOptional, IsString } from 'class-validator'
|
||||
import { AccountStatus, AccountType } from 'generated/prisma/enums'
|
||||
import { CreateUserDto } from '../../../users/dto/user.dto'
|
||||
|
||||
export class CreateAccountDto extends CreateUserDto {
|
||||
@IsString()
|
||||
username: string
|
||||
|
||||
@IsEnum(AccountStatus)
|
||||
status: AccountStatus
|
||||
|
||||
@IsString()
|
||||
password: string
|
||||
}
|
||||
|
||||
export class UpdateAccountDto {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
username?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(AccountType)
|
||||
type?: AccountType
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(AccountStatus)
|
||||
status?: AccountStatus
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
password?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
user_id?: string
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { Body, Controller, Delete, Get, Param, Post, Put } from '@nestjs/common'
|
||||
import { CreatePartnerDto, UpdatePartnerDto } from './dto/partner.dto'
|
||||
import { PartnersService } from './partners.service'
|
||||
|
||||
@Controller('admin/partners')
|
||||
export class PartnersController {
|
||||
constructor(private readonly partnersService: PartnersService) {}
|
||||
|
||||
@Get()
|
||||
async findAll() {
|
||||
return this.partnersService.findAll()
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async findOne(@Param('id') id: string) {
|
||||
return this.partnersService.findOne(id)
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(@Body() data: CreatePartnerDto) {
|
||||
return this.partnersService.create(data)
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
async update(@Param('id') id: string, @Body() data: UpdatePartnerDto) {
|
||||
return this.partnersService.update(id, data)
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
async delete(@Param('id') id: string) {
|
||||
return this.partnersService.delete(id)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { PrismaModule } from '@/prisma/prisma.module'
|
||||
import { Module } from '@nestjs/common'
|
||||
|
||||
import { AdminPartnerLicensesModule } from './licenses/licenses.module'
|
||||
import { AdminPartnerAccountsModule } from './partnerAccounts/accounts.module'
|
||||
import { PartnersController } from './partners.controller'
|
||||
import { PartnersService } from './partners.service'
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule, AdminPartnerLicensesModule, AdminPartnerAccountsModule],
|
||||
controllers: [PartnersController],
|
||||
providers: [PartnersService],
|
||||
exports: [PartnersService],
|
||||
})
|
||||
export class AdminPartnersModule {}
|
||||
@@ -0,0 +1,36 @@
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreatePartnerDto, UpdatePartnerDto } from './dto/partner.dto'
|
||||
|
||||
@Injectable()
|
||||
export class PartnersService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async findAll() {
|
||||
const partners = await this.prisma.partner.findMany()
|
||||
return ResponseMapper.list(partners)
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const partner = await this.prisma.partner.findUnique({
|
||||
where: { id },
|
||||
})
|
||||
return ResponseMapper.single(partner)
|
||||
}
|
||||
|
||||
async create(data: CreatePartnerDto) {
|
||||
const partner = await this.prisma.partner.create({ data })
|
||||
return ResponseMapper.create(partner)
|
||||
}
|
||||
|
||||
async update(id: string, data: UpdatePartnerDto) {
|
||||
const partner = await this.prisma.partner.update({ where: { id }, data })
|
||||
return ResponseMapper.update(partner)
|
||||
}
|
||||
|
||||
async delete(id: string) {
|
||||
await this.prisma.partner.delete({ where: { id } })
|
||||
return ResponseMapper.delete()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { IsOptional, IsString } from 'class-validator'
|
||||
|
||||
export class CreateProviderDto {
|
||||
@IsString()
|
||||
name: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
code?: string
|
||||
}
|
||||
|
||||
export class UpdateProviderDto {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
name?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
code?: string
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { Body, Controller, Delete, Get, Param, Post, Put } from '@nestjs/common'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
import { CreateProviderDto, UpdateProviderDto } from './dto/provider.dto'
|
||||
import { ProvidersService } from './providers.service'
|
||||
|
||||
@ApiTags('Admin Partners')
|
||||
@Controller('admin/providers')
|
||||
export class ProvidersController {
|
||||
constructor(private readonly providersService: ProvidersService) {}
|
||||
|
||||
@Get()
|
||||
async findAll() {
|
||||
return this.providersService.findAll()
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async findOne(@Param('id') id: string) {
|
||||
return this.providersService.findOne(id)
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(@Body() data: CreateProviderDto) {
|
||||
return this.providersService.create(data)
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
async update(@Param('id') id: string, @Body() data: UpdateProviderDto) {
|
||||
return this.providersService.update(id, data)
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
async delete(@Param('id') id: string) {
|
||||
return this.providersService.delete(id)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { PrismaModule } from '@/prisma/prisma.module'
|
||||
import { Module } from '@nestjs/common'
|
||||
import { ProvidersController } from './providers.controller'
|
||||
import { ProvidersService } from './providers.service'
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [ProvidersController],
|
||||
providers: [ProvidersService],
|
||||
exports: [ProvidersService],
|
||||
})
|
||||
export class AdminProvidersModule {}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreateProviderDto, UpdateProviderDto } from './dto/provider.dto'
|
||||
|
||||
@Injectable()
|
||||
export class ProvidersService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async findAll() {
|
||||
const providers = await this.prisma.provider.findMany()
|
||||
return ResponseMapper.list(providers)
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const provider = await this.prisma.provider.findUnique({
|
||||
where: { id },
|
||||
include: { pos_list: true, accounts: true },
|
||||
})
|
||||
return ResponseMapper.single(provider)
|
||||
}
|
||||
|
||||
async create(data: CreateProviderDto) {
|
||||
const provider = await this.prisma.provider.create({ data })
|
||||
return ResponseMapper.create(provider)
|
||||
}
|
||||
|
||||
async update(id: string, data: UpdateProviderDto) {
|
||||
const provider = await this.prisma.provider.update({ where: { id }, data })
|
||||
return ResponseMapper.update(provider)
|
||||
}
|
||||
|
||||
async delete(id: string) {
|
||||
await this.prisma.provider.delete({ where: { id } })
|
||||
return ResponseMapper.delete()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Controller, Get } from '@nestjs/common'
|
||||
import { TranslateService } from './translate.service'
|
||||
|
||||
@Controller('admin/translates')
|
||||
export class TranslateController {
|
||||
constructor(private readonly translateService: TranslateService) {}
|
||||
|
||||
@Get()
|
||||
async getTranslations() {
|
||||
return this.translateService.getTranslations()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { Module } from '@nestjs/common'
|
||||
import { TranslateController } from './translate.controller'
|
||||
import { TranslateService } from './translate.service'
|
||||
|
||||
@Module({
|
||||
controllers: [TranslateController],
|
||||
providers: [TranslateService],
|
||||
exports: [TranslateService],
|
||||
})
|
||||
export class AdminTranslateModule {}
|
||||
@@ -0,0 +1,68 @@
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
|
||||
@Injectable()
|
||||
export class TranslateService {
|
||||
getTranslations() {
|
||||
return ResponseMapper.single({
|
||||
attributes: {
|
||||
id: 'شناسه',
|
||||
username: 'نام کاربری',
|
||||
status: 'وضعیت',
|
||||
created_at: 'تاریخ ایجاد',
|
||||
updated_at: 'تاریخ بروزرسانی',
|
||||
actions: 'عملیات',
|
||||
first_name: 'نام',
|
||||
last_name: 'نام خانوادگی',
|
||||
mobile_number: 'تلفن همراه',
|
||||
national_code: 'کد ملی',
|
||||
name: 'عنوان',
|
||||
os_version: 'نسخه سیستم عامل',
|
||||
code: 'کد شناسایی',
|
||||
fullname: 'نام کامل',
|
||||
'brand.name': 'عنوان برند',
|
||||
'pos.name': 'عنوان صنف',
|
||||
'pos.complex.name': 'عنوان مجموعه',
|
||||
'pos.devices.name': 'عنوان دستگاه',
|
||||
'pos.provider.name': 'عنوان ارایهدهنده',
|
||||
goods_count: 'تعداد کالاها',
|
||||
brand: 'برند',
|
||||
licenses: 'لایسنس',
|
||||
business_activity: 'فعالیت اقتصادی',
|
||||
user: 'کاربر',
|
||||
},
|
||||
singular: {
|
||||
users: 'کاربر',
|
||||
devices: 'دستگاه',
|
||||
device_brands: 'برند',
|
||||
guilds: 'صنف',
|
||||
guild_goods: 'کالا',
|
||||
guild_good_categories: 'دستهبندی کالا',
|
||||
partners: 'شریک تجاری',
|
||||
partner_accounts: 'حساب',
|
||||
partner_licenses: 'مجوز',
|
||||
providers: 'ارائهدهنده',
|
||||
provider_accounts: 'حساب',
|
||||
accounts: 'حساب کاربری',
|
||||
licenses: 'لایسنس',
|
||||
business_activities: 'فعالیت اقتصادی',
|
||||
},
|
||||
plural: {
|
||||
users: 'کاربران',
|
||||
devices: 'دستگاهها',
|
||||
device_brands: 'برندها',
|
||||
guilds: 'اصناف',
|
||||
guild_goods: 'کالاها',
|
||||
guild_good_categories: 'دستهبندی کالاها',
|
||||
partners: 'شرکای تجاری',
|
||||
partner_accounts: 'حسابهای شریک تجاری',
|
||||
partner_licenses: 'مجوزهای داده شده',
|
||||
providers: 'ارائهدهندگان',
|
||||
provider_accounts: 'حسابهای ارائهدهنده',
|
||||
accounts: 'حسابهای کاربری',
|
||||
licenses: 'لایسنسها',
|
||||
business_activities: 'فعالیتهای اقتصادی',
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { Body, Controller, Delete, Get, Param, Post, Put } from '@nestjs/common'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
import { AccountsService } from './accounts.service'
|
||||
import { CreateAccountDto, UpdateAccountDto } from './dto/account.dto'
|
||||
|
||||
@ApiTags('AdminAccounts')
|
||||
@Controller('admin/users/:userId/accounts')
|
||||
export class AccountsController {
|
||||
constructor(private readonly accountsService: AccountsService) {}
|
||||
|
||||
@Get()
|
||||
async findAll(@Param('userId') userId: string) {
|
||||
return this.accountsService.findAll(userId)
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async findOne(@Param('id') id: string) {
|
||||
return this.accountsService.findOne(id)
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(@Body() data: CreateAccountDto) {
|
||||
return this.accountsService.create(data)
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
async update(@Param('id') id: string, @Body() data: UpdateAccountDto) {
|
||||
return this.accountsService.update(id, data)
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
async delete(@Param('id') id: string) {
|
||||
return this.accountsService.delete(id)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { PrismaModule } from '@/prisma/prisma.module'
|
||||
import { Module } from '@nestjs/common'
|
||||
import { AccountsController } from './accounts.controller'
|
||||
import { AccountsService } from './accounts.service'
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [AccountsController],
|
||||
providers: [AccountsService],
|
||||
exports: [AccountsService],
|
||||
})
|
||||
export class AdminUserAccountsModule {}
|
||||
@@ -0,0 +1,47 @@
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreateAccountDto, UpdateAccountDto } from './dto/account.dto'
|
||||
|
||||
@Injectable()
|
||||
export class AccountsService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async findAll(userId: string) {
|
||||
const accounts = await this.prisma.account.findMany({
|
||||
where: { user_id: userId },
|
||||
omit: { password: true, user_id: true },
|
||||
})
|
||||
return ResponseMapper.list(accounts)
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const account = await this.prisma.account.findUnique({
|
||||
where: { id },
|
||||
omit: { password: true, user_id: true },
|
||||
include: {
|
||||
pos: true,
|
||||
business: true,
|
||||
provider: true,
|
||||
partner: true,
|
||||
},
|
||||
})
|
||||
return ResponseMapper.single(account)
|
||||
}
|
||||
|
||||
async create(data: CreateAccountDto) {
|
||||
// Assuming password hashing is handled elsewhere or add bcrypt here
|
||||
const account = await this.prisma.account.create({ data })
|
||||
return ResponseMapper.create(account)
|
||||
}
|
||||
|
||||
async update(id: string, data: UpdateAccountDto) {
|
||||
const account = await this.prisma.account.update({ where: { id }, data })
|
||||
return ResponseMapper.update(account)
|
||||
}
|
||||
|
||||
async delete(id: string) {
|
||||
await this.prisma.account.delete({ where: { id } })
|
||||
return ResponseMapper.delete()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import { IsEnum, IsOptional, IsString } from 'class-validator'
|
||||
import { AccountStatus, AccountType } from 'generated/prisma/enums'
|
||||
|
||||
export class CreateAccountDto {
|
||||
@IsString()
|
||||
username: string
|
||||
|
||||
@IsEnum(AccountType)
|
||||
type: AccountType
|
||||
|
||||
@IsEnum(AccountStatus)
|
||||
status: AccountStatus
|
||||
|
||||
@IsString()
|
||||
password: string
|
||||
|
||||
@IsString()
|
||||
user_id: string
|
||||
}
|
||||
|
||||
export class UpdateAccountDto {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
username?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(AccountType)
|
||||
type?: AccountType
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(AccountStatus)
|
||||
status?: AccountStatus
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
password?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
user_id?: string
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import { Body, Controller, Get, Param, Post } from '@nestjs/common'
|
||||
import { BusinessActivitiesService } from './business-activities.service'
|
||||
import { CreateBusinessActivitiesDto } from './dto/create-business-activities.dto'
|
||||
|
||||
@Controller('admin/users/:userId/business_activities')
|
||||
export class BusinessActivitiesController {
|
||||
constructor(private readonly businessActivitiesService: BusinessActivitiesService) {}
|
||||
|
||||
@Get()
|
||||
async findAll(@Param('userId') userId: string) {
|
||||
return this.businessActivitiesService.findAll(userId)
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async findOne(@Param('userId') userId: string, @Param('id') id: string) {
|
||||
return this.businessActivitiesService.findOne(userId, id)
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(
|
||||
@Param('userId') userId: string,
|
||||
@Body() data: CreateBusinessActivitiesDto,
|
||||
) {
|
||||
return this.businessActivitiesService.create(userId, data)
|
||||
}
|
||||
|
||||
// @Put(':id')
|
||||
// async update(
|
||||
// @Param('userId') userId: string,
|
||||
// @Param('id') id: string,
|
||||
// @Body() data: UpdateBusinessActivityDto,
|
||||
// ) {
|
||||
// return this.businessActivitiesService.update(userId, id, data)
|
||||
// }
|
||||
|
||||
// @Delete(':id')
|
||||
// async delete(@Param('id') id: string) {
|
||||
// return this.businessActivitiesService.delete(id)
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { PrismaModule } from '@/prisma/prisma.module'
|
||||
import { Module } from '@nestjs/common'
|
||||
import { BusinessActivitiesController } from './business-activities.controller'
|
||||
import { BusinessActivitiesService } from './business-activities.service'
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [BusinessActivitiesController],
|
||||
providers: [BusinessActivitiesService],
|
||||
})
|
||||
export class AdminUserBusinessActivitiesModule {}
|
||||
@@ -0,0 +1,57 @@
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreateBusinessActivitiesDto } from './dto/create-business-activities.dto'
|
||||
|
||||
@Injectable()
|
||||
export class BusinessActivitiesService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async findAll(userId: string) {
|
||||
const businessActivities = await this.prisma.businessActivity.findMany({
|
||||
where: {
|
||||
owner_id: userId,
|
||||
},
|
||||
})
|
||||
return ResponseMapper.list(businessActivities)
|
||||
}
|
||||
|
||||
async findOne(userId: string, id: string) {
|
||||
const businessActivity = await this.prisma.businessActivity.findUnique({
|
||||
where: { owner_id: userId, id },
|
||||
})
|
||||
return ResponseMapper.single(businessActivity)
|
||||
}
|
||||
|
||||
async create(userId: string, data: CreateBusinessActivitiesDto) {
|
||||
const businessActivity = await this.prisma.businessActivity.create({
|
||||
data: {
|
||||
name: data.name,
|
||||
guild: {
|
||||
connect: {
|
||||
id: data.guild_id,
|
||||
},
|
||||
},
|
||||
user: {
|
||||
connect: {
|
||||
id: userId,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
return ResponseMapper.create(businessActivity)
|
||||
}
|
||||
|
||||
async update(id: string, data: any) {
|
||||
const businessActivity = await this.prisma.businessActivity.update({
|
||||
where: { id },
|
||||
data,
|
||||
})
|
||||
return ResponseMapper.update(businessActivity)
|
||||
}
|
||||
|
||||
async delete(id: string) {
|
||||
await this.prisma.businessActivity.delete({ where: { id } })
|
||||
return ResponseMapper.delete()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { ApiProperty } from '@nestjs/swagger'
|
||||
import { IsString } from 'class-validator'
|
||||
|
||||
export class CreateBusinessActivitiesDto {
|
||||
@IsString()
|
||||
@ApiProperty({})
|
||||
name: string
|
||||
|
||||
@IsString()
|
||||
@ApiProperty({})
|
||||
guild_id: string
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/swagger'
|
||||
import { CreateBusinessActivitiesDto } from './create-business-activities.dto'
|
||||
|
||||
export class UpdateBusinessActivityDto extends PartialType(CreateBusinessActivitiesDto) {}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { IsString } from 'class-validator'
|
||||
|
||||
export class CreateUserDto {
|
||||
@IsString()
|
||||
mobile_number: string
|
||||
|
||||
@IsString()
|
||||
national_code?: string
|
||||
|
||||
@IsString()
|
||||
first_name: string
|
||||
|
||||
@IsString()
|
||||
last_name: string
|
||||
}
|
||||
export class UpdateUserDto {
|
||||
@IsString()
|
||||
mobile_number: string
|
||||
|
||||
@IsString()
|
||||
national_code?: string
|
||||
|
||||
@IsString()
|
||||
first_name?: string
|
||||
|
||||
@IsString()
|
||||
last_name?: string
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { Body, Controller, Delete, Get, Param, Post, Put } from '@nestjs/common'
|
||||
import { CreateUserDto, UpdateUserDto } from './dto/user.dto'
|
||||
import { AdminUsersService } from './users.service'
|
||||
|
||||
@Controller('admin/users')
|
||||
export class AdminUsersController {
|
||||
constructor(private readonly usersService: AdminUsersService) {}
|
||||
|
||||
@Get()
|
||||
async findAll() {
|
||||
return this.usersService.findAll()
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async findOne(@Param('id') id: string) {
|
||||
return this.usersService.findOne(id)
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(@Body() data: any) {
|
||||
return this.usersService.create(data as CreateUserDto)
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
async update(@Param('id') id: string, @Body() data: any) {
|
||||
return this.usersService.update(id, data as UpdateUserDto)
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
async delete(@Param('id') id: string) {
|
||||
return this.usersService.delete(id)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { PrismaModule } from '@/prisma/prisma.module'
|
||||
import { Module } from '@nestjs/common'
|
||||
import { AdminUserAccountsModule } from './accounts/accounts.module'
|
||||
import { AdminUserBusinessActivitiesModule } from './business-activities/business-activities.module'
|
||||
import { AdminUsersController } from './users.controller'
|
||||
import { AdminUsersService } from './users.service'
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule, AdminUserAccountsModule, AdminUserBusinessActivitiesModule],
|
||||
controllers: [AdminUsersController],
|
||||
providers: [AdminUsersService],
|
||||
exports: [AdminUserAccountsModule],
|
||||
})
|
||||
export class AdminUsersModule {}
|
||||
@@ -0,0 +1,42 @@
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreateUserDto, UpdateUserDto } from './dto/user.dto'
|
||||
|
||||
@Injectable()
|
||||
export class AdminUsersService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async findAll() {
|
||||
const [users, count] = await this.prisma.$transaction([
|
||||
this.prisma.user.findMany(),
|
||||
this.prisma.user.count(),
|
||||
])
|
||||
return ResponseMapper.paginate(
|
||||
users.map(user => ({ ...user, fullname: `${user.first_name} ${user.last_name}` })),
|
||||
{ count },
|
||||
)
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const user = await this.prisma.user.findUnique({
|
||||
where: { id },
|
||||
})
|
||||
return ResponseMapper.single({
|
||||
...user,
|
||||
fullname: `${user?.first_name} ${user?.last_name}`,
|
||||
})
|
||||
}
|
||||
|
||||
async create(data: CreateUserDto) {
|
||||
return this.prisma.user.create({ data })
|
||||
}
|
||||
|
||||
async update(id: string, data: UpdateUserDto) {
|
||||
return this.prisma.user.update({ where: { id }, data: data as UpdateUserDto })
|
||||
}
|
||||
|
||||
async delete(id: string) {
|
||||
return this.prisma.user.delete({ where: { id } })
|
||||
}
|
||||
}
|
||||
@@ -1,46 +1,31 @@
|
||||
import { Controller, Get } from '@nestjs/common'
|
||||
import { Public } from '@/common/decorators/public.decorator'
|
||||
import { Body, Controller, Get, Post, Res } from '@nestjs/common'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
import { AuthService } from './auth.service'
|
||||
import { reqTokenPayload } from './current-user.decorator'
|
||||
import { LoginDto } from './dto/login.dto'
|
||||
|
||||
@ApiTags('auth')
|
||||
@Controller('auth')
|
||||
export class AuthController {
|
||||
constructor(private readonly auth: AuthService) {}
|
||||
|
||||
// @Public()
|
||||
// @Post('send_code')
|
||||
// async sendCode(@Body() dto: SendCodeDto) {
|
||||
// return this.auth.sendCode(dto)
|
||||
// }
|
||||
|
||||
// @Public()
|
||||
// @Post('login')
|
||||
// async login(@Body() dto: VerifyCodeDto, @Res({ passthrough: true }) res) {
|
||||
// const result = await this.auth.loginWithCode(dto)
|
||||
// // Set accessToken as httpOnly cookie
|
||||
// // @ts-ignore
|
||||
// if (result?.data?.accessToken) {
|
||||
// // @ts-ignore
|
||||
// res.cookie('accessToken', result.data.accessToken, {
|
||||
// httpOnly: true,
|
||||
// secure: process.env.NODE_ENV === 'production',
|
||||
// sameSite: 'lax',
|
||||
// maxAge: 1000 * 60 * 60 * 24 * 7, // 7 days
|
||||
// })
|
||||
// }
|
||||
// return result
|
||||
// }
|
||||
|
||||
// @Post('refresh')
|
||||
// async refresh(@Body() dto: RefreshTokenDto) {
|
||||
// return this.auth.refreshToken(dto)
|
||||
// }
|
||||
|
||||
// @Post('logout')
|
||||
// async logout(@Body() dto: RefreshTokenDto) {
|
||||
// return this.auth.logout(dto.refreshToken)
|
||||
// }
|
||||
@Public()
|
||||
@Post('login')
|
||||
async login(@Body() dto: LoginDto, @Res({ passthrough: true }) res): Promise<any> {
|
||||
const result = await this.auth.login(dto)
|
||||
// @ts-ignore
|
||||
if (result?.data?.accessToken) {
|
||||
// @ts-ignore
|
||||
res.cookie('accessToken', result.data.accessToken, {
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV === 'production',
|
||||
sameSite: 'lax',
|
||||
maxAge: 1000 * 60 * 60 * 24 * 7, // 7 days
|
||||
})
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
@Get('me')
|
||||
async me(@reqTokenPayload() user: any) {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { PrismaModule } from '@/prisma/prisma.module'
|
||||
import { Module } from '@nestjs/common'
|
||||
import { JwtModule } from '@nestjs/jwt'
|
||||
import 'dotenv/config'
|
||||
import { env } from 'prisma/config'
|
||||
import { PrismaModule } from '../../prisma/prisma.module'
|
||||
import { AuthController } from './auth.controller'
|
||||
import { AuthService } from './auth.service'
|
||||
|
||||
|
||||
@@ -1,163 +1,115 @@
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { PasswordUtil } from '@/common/utils/password.util'
|
||||
import { Account, Complex, License, User } from '@/generated/prisma/client'
|
||||
import { TokenType } from '@/generated/prisma/enums'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable, NotFoundException } from '@nestjs/common'
|
||||
import { JwtService } from '@nestjs/jwt'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { createHash, randomBytes } from 'crypto'
|
||||
import 'dotenv/config'
|
||||
import { ResponseMapper } from '../../common/response/response-mapper'
|
||||
import { PrismaService } from '../../prisma/prisma.service'
|
||||
import { env } from 'prisma/config'
|
||||
import { LoginDto } from './dto/login.dto'
|
||||
import { AccessTokenPayload } from './models'
|
||||
|
||||
interface IGenerateLoginResponse extends Account {
|
||||
user: User
|
||||
}
|
||||
|
||||
interface IPosAccountResponse extends Partial<IGenerateLoginResponse> {
|
||||
complex?: Complex
|
||||
license?: Partial<License>
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
// private readonly OTP_TTL_MINUTES = 2
|
||||
// private readonly ACCESS_TOKEN_EXP = Number(env('JWT_EXPIRES_IN')) || '150m'
|
||||
// private readonly REFRESH_TOKEN_EXP_DAYS = 30
|
||||
private readonly OTP_TTL_MINUTES = 2
|
||||
private readonly ACCESS_TOKEN_EXP = Number(env('JWT_EXPIRES_IN')) || '12h'
|
||||
private readonly REFRESH_TOKEN_EXP_DAYS = 30
|
||||
|
||||
constructor(
|
||||
private prisma: PrismaService,
|
||||
private jwt: JwtService,
|
||||
) {}
|
||||
|
||||
// private generateCode() {
|
||||
// // For development you may want fixed code via env, otherwise random 5-digit
|
||||
// const staticCode = process.env.OTP_STATIC_CODE
|
||||
// if (staticCode) return staticCode
|
||||
// return Math.floor(10000 + Math.random() * 90000).toString()
|
||||
// }
|
||||
private async generateLoginResponse(account: IGenerateLoginResponse) {
|
||||
const { password, ...accountData } = account
|
||||
const preparedAccount = { ...accountData } as IPosAccountResponse
|
||||
|
||||
// // @TODO: we must detect context of request to verify user need to logged in as which role (admin, user, etc) and generate token with that role.
|
||||
// async sendCode(dto: SendCodeDto) {
|
||||
// const { mobile_number } = dto
|
||||
// const account = await this.prisma.account.findFirst({
|
||||
// where: {
|
||||
// user: {
|
||||
// mobile_number,
|
||||
// },
|
||||
// },
|
||||
// })
|
||||
// console.log(mobile_number)
|
||||
const signTokenData = {
|
||||
user_id: account.user.id,
|
||||
mobile_number: account.user.mobile_number,
|
||||
type: account.type,
|
||||
username: account.username,
|
||||
account_id: account.id,
|
||||
} as AccessTokenPayload
|
||||
|
||||
// if (!account) {
|
||||
// throw new NotFoundException('کاربری با این شماره تماس یافت نشد')
|
||||
// }
|
||||
if (account.pos_id) {
|
||||
const pos = await this.prisma.pos.findUnique({
|
||||
where: { id: account.pos_id },
|
||||
include: { complex: true, licenses: true },
|
||||
})
|
||||
|
||||
// const existingCode = await this.prisma.verificationCode.findFirst({
|
||||
// where: { account_id: account.id, expires_at: { gt: new Date() } },
|
||||
// })
|
||||
// if (existingCode) {
|
||||
// throw new BadRequestException('کد برای شما ارسال شده است')
|
||||
// }
|
||||
if (!pos) throw new NotFoundException('متاسفانه کاربر شما یافت نشد')
|
||||
Object.assign(signTokenData, {
|
||||
pos_id: pos.id,
|
||||
pos_name: pos.serial,
|
||||
complex_id: pos.complex_id,
|
||||
license_id: pos.licenses[0].id,
|
||||
license_expired_at: pos.licenses[0].expires_at,
|
||||
})
|
||||
|
||||
// const code = this.generateCode()
|
||||
// const expires_at = new Date(Date.now() + this.OTP_TTL_MINUTES * 60 * 1000)
|
||||
const { partner_id, pos_id, ...license } = pos.licenses[0]
|
||||
preparedAccount.license = license
|
||||
preparedAccount.complex = pos.complex
|
||||
}
|
||||
|
||||
// await this.prisma.verificationCode.create({
|
||||
// data: {
|
||||
// account_id: account.id,
|
||||
// code,
|
||||
// expires_at,
|
||||
// },
|
||||
// })
|
||||
const accessToken = this.jwt.sign(signTokenData, { expiresIn: this.ACCESS_TOKEN_EXP })
|
||||
const refreshTokenPlain = randomBytes(48).toString('hex')
|
||||
const tokenHash = createHash('sha256').update(refreshTokenPlain).digest('hex')
|
||||
const expires_at = new Date(
|
||||
Date.now() + this.REFRESH_TOKEN_EXP_DAYS * 24 * 60 * 60 * 1000,
|
||||
)
|
||||
|
||||
// // TODO: integrate SMS provider. For now return the code for dev.
|
||||
// return {
|
||||
// ok: true,
|
||||
// }
|
||||
// }
|
||||
await this.prisma.token.upsert({
|
||||
where: { type_account_id: { type: TokenType.ACCESS, account_id: account.id } },
|
||||
update: { token: tokenHash, expires_at },
|
||||
create: {
|
||||
token: tokenHash,
|
||||
account_id: account.id,
|
||||
expires_at,
|
||||
type: TokenType.ACCESS,
|
||||
},
|
||||
})
|
||||
|
||||
// async loginWithCode(dto: VerifyCodeDto) {
|
||||
// const { mobile_number, code } = dto
|
||||
// const account = await this.prisma.account.findFirst({
|
||||
// where: { user: { mobile_number } },
|
||||
// include: {
|
||||
// user: true,
|
||||
// },
|
||||
// })
|
||||
// if (!account) {
|
||||
// throw new NotFoundException('کاربری با این شماره تماس یافت نشد')
|
||||
// }
|
||||
// const otp = await this.prisma.verificationCode.findFirst({
|
||||
// where: { account_id: account.id },
|
||||
// })
|
||||
// if (!otp) throw new BadRequestException('اطلاعات ورودی درست نیست')
|
||||
// // if (otp.expires_at) throw new BadRequestException('کد قبلن استفاده شده است')
|
||||
// if (otp.expires_at.getTime() < Date.now())
|
||||
// throw new BadRequestException('کد وارد شده منقضی شده است')
|
||||
// if (otp.code !== code) throw new BadRequestException('کد وارد شده اشتباه است')
|
||||
// // mark used
|
||||
return ResponseMapper.single({
|
||||
accessToken,
|
||||
refreshToken: refreshTokenPlain,
|
||||
account: preparedAccount,
|
||||
})
|
||||
}
|
||||
|
||||
// const signTokenData = {
|
||||
// sub: account.user.id,
|
||||
// mobile_number: account.user.mobile_number,
|
||||
// type: account.type,
|
||||
// username: account.username,
|
||||
// }
|
||||
async login(dto: LoginDto) {
|
||||
const { username, password } = dto
|
||||
|
||||
// if (account.pos_id) {
|
||||
// const pos = await this.prisma.pos.findUnique({
|
||||
// where: { id: account.pos_id },
|
||||
// include: {
|
||||
// complex: true,
|
||||
// licenses: true,
|
||||
// },
|
||||
// })
|
||||
// if (!pos) {
|
||||
// throw new NotFoundException('متاسفانه کاربر شما یافت نشد')
|
||||
// }
|
||||
// Object.assign(signTokenData, {
|
||||
// pos_id: pos.id,
|
||||
// pos_name: pos.serial,
|
||||
// complex: pos.complex,
|
||||
// license: pos.licenses[0],
|
||||
// })
|
||||
// }
|
||||
// console.log(signTokenData)
|
||||
const account = await this.prisma.account.findUnique({
|
||||
where: { username },
|
||||
include: {
|
||||
user: true,
|
||||
},
|
||||
})
|
||||
|
||||
// const accessToken = this.jwt.sign(signTokenData, { expiresIn: this.ACCESS_TOKEN_EXP })
|
||||
// const refreshTokenPlain = randomBytes(48).toString('hex')
|
||||
// const tokenHash = createHash('sha256').update(refreshTokenPlain).digest('hex')
|
||||
// const expires_at = new Date(
|
||||
// Date.now() + this.REFRESH_TOKEN_EXP_DAYS * 24 * 60 * 60 * 1000,
|
||||
// )
|
||||
// await this.prisma.token.upsert({
|
||||
// where: { type_account_id: { type: TokenType.ACCESS, account_id: account.id } },
|
||||
// update: { token: tokenHash, expires_at },
|
||||
// create: {
|
||||
// token: tokenHash,
|
||||
// account_id: account.id,
|
||||
// expires_at,
|
||||
// type: TokenType.ACCESS,
|
||||
// },
|
||||
// })
|
||||
// await this.prisma.verificationCode.delete({ where: { id: otp.id } })
|
||||
if (!account) {
|
||||
throw new NotFoundException('اطلاعات ورودی شما اشتباه است')
|
||||
}
|
||||
|
||||
// return ResponseMapper.single({
|
||||
// accessToken,
|
||||
// refreshToken: refreshTokenPlain,
|
||||
// account,
|
||||
// })
|
||||
// }
|
||||
const isMatch = await PasswordUtil.compare(password, account.password)
|
||||
|
||||
// // async refreshToken(dto: RefreshTokenDto) {
|
||||
// // const { refreshToken } = dto
|
||||
// // const tokenHash = createHash('sha256').update(refreshToken).digest('hex')
|
||||
// // const record = await this.prisma.refreshToken.findFirst({ where: { tokenHash } })
|
||||
// // if (!record || record.revoked || record.expiresAt.getTime() < Date.now()) {
|
||||
// // throw new UnauthorizedException('Invalid refresh token')
|
||||
// // }
|
||||
// // const user = await this.prisma.user.findUnique({ where: { id: record.userId } })
|
||||
// // if (!user) throw new UnauthorizedException('User not found')
|
||||
// // const accessToken = this.jwt.sign(
|
||||
// // { sub: user.id, mobileNumber: user.mobileNumber },
|
||||
// // { expiresIn: this.ACCESS_TOKEN_EXP },
|
||||
// // )
|
||||
// // return { accessToken }
|
||||
// // }
|
||||
if (!isMatch) {
|
||||
throw new NotFoundException('اطلاعات ورودی شما اشتباه است')
|
||||
}
|
||||
|
||||
// // async logout() {
|
||||
// // await this.prisma.token.deleteMany({
|
||||
// // where: { tokenHash },
|
||||
// // data: { revoked: true },
|
||||
// // })
|
||||
// // return { ok: true }
|
||||
// // }
|
||||
return await this.generateLoginResponse(account)
|
||||
}
|
||||
|
||||
async me(username: string) {
|
||||
return ResponseMapper.single({
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { createParamDecorator, ExecutionContext } from '@nestjs/common'
|
||||
import { AccessTokenPayload } from '../../common/models/token-model'
|
||||
import { AccessTokenPayload } from 'common/models/token-model'
|
||||
|
||||
export const reqTokenPayload = createParamDecorator(
|
||||
(data: unknown, ctx: ExecutionContext) => {
|
||||
|
||||
@@ -4,7 +4,7 @@ import { IsString } from 'class-validator'
|
||||
export class LoginDto {
|
||||
@ApiProperty({ description: 'Mobile number used as username', example: '09120258156' })
|
||||
@IsString()
|
||||
mobileNumber: string
|
||||
username: string
|
||||
|
||||
@ApiProperty({ description: 'OTP password (one-time password)', example: '11111' })
|
||||
@IsString()
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
import {
|
||||
CanActivate,
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common'
|
||||
import { Reflector } from '@nestjs/core'
|
||||
import { JwtService } from '@nestjs/jwt'
|
||||
import { IS_PUBLIC_KEY } from '../../common/decorators/public.decorator'
|
||||
import { IWithJWTPayloadRequest } from '../../common/models/token-model'
|
||||
|
||||
@Injectable()
|
||||
export class JwtAuthGuard implements CanActivate {
|
||||
constructor(
|
||||
private jwt: JwtService,
|
||||
private reflector: Reflector,
|
||||
) {}
|
||||
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
|
||||
context.getHandler(),
|
||||
context.getClass(),
|
||||
])
|
||||
if (isPublic) return true
|
||||
|
||||
const req = context.switchToHttp().getRequest<IWithJWTPayloadRequest>()
|
||||
|
||||
let token: string | undefined = req.cookies?.accessToken
|
||||
if (!token) {
|
||||
const authHeader = (req.headers.authorization || req.headers.Authorization) as
|
||||
| string
|
||||
| undefined
|
||||
if (authHeader && authHeader.startsWith('Bearer ')) {
|
||||
token = authHeader.slice(7)
|
||||
}
|
||||
}
|
||||
|
||||
if (!token) throw new UnauthorizedException('Missing access token')
|
||||
|
||||
try {
|
||||
const payload = this.jwt.verify(token, {
|
||||
secret: process.env.JWT_SECRET || 'secret',
|
||||
})
|
||||
|
||||
if (payload.type !== 'POS')
|
||||
throw new UnauthorizedException('Invalid or expired token')
|
||||
|
||||
// Set the typed dataPayload to the request
|
||||
req.dataPayload = payload
|
||||
|
||||
return true
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
throw new UnauthorizedException('Invalid or expired token')
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { Request as ExpressRequest } from 'express'
|
||||
import { AccountType } from '../../../generated/prisma/enums'
|
||||
|
||||
export interface IWithJWTPayloadRequest extends ExpressRequest {
|
||||
dataPayload?: AccessTokenPayload
|
||||
}
|
||||
|
||||
export interface AccessTokenPayload {
|
||||
account_id: string
|
||||
user_id: string
|
||||
mobile_number: string
|
||||
type: AccountType
|
||||
username: string
|
||||
pos_id?: number
|
||||
pos_name?: string
|
||||
complex_id?: string
|
||||
license_id?: string
|
||||
license_expired_at?: string
|
||||
}
|
||||
@@ -9,4 +9,84 @@ export class EnumsController {
|
||||
async getGoldKarat() {
|
||||
return this.enumsService.getEnumValues('GoldKarat')
|
||||
}
|
||||
|
||||
@Get('user_status')
|
||||
async getUserStatus() {
|
||||
return this.enumsService.getEnumValues('UserStatus')
|
||||
}
|
||||
|
||||
@Get('account_role')
|
||||
async getAccountRole() {
|
||||
return this.enumsService.getEnumValues('AccountRole')
|
||||
}
|
||||
|
||||
@Get('account_status')
|
||||
async getAccountStatus() {
|
||||
return this.enumsService.getEnumValues('AccountStatus')
|
||||
}
|
||||
|
||||
@Get('pos_status')
|
||||
async getPOSStatus() {
|
||||
return this.enumsService.getEnumValues('POSStatus')
|
||||
}
|
||||
|
||||
@Get('pos_role')
|
||||
async getPOSRole() {
|
||||
return this.enumsService.getEnumValues('POSRole')
|
||||
}
|
||||
|
||||
@Get('license_type')
|
||||
async getLicenseType() {
|
||||
return this.enumsService.getEnumValues('LicenseType')
|
||||
}
|
||||
|
||||
@Get('license_status')
|
||||
async getLicenseStatus() {
|
||||
return this.enumsService.getEnumValues('LicenseStatus')
|
||||
}
|
||||
|
||||
@Get('pos_type')
|
||||
async getPOSType() {
|
||||
return this.enumsService.getEnumValues('POSType')
|
||||
}
|
||||
|
||||
@Get('user_type')
|
||||
async getUserType() {
|
||||
return this.enumsService.getEnumValues('UserType')
|
||||
}
|
||||
|
||||
@Get('account_type')
|
||||
async getAccountType() {
|
||||
return this.enumsService.getEnumValues('AccountType')
|
||||
}
|
||||
|
||||
@Get('partner_role')
|
||||
async getPartnerRole() {
|
||||
return this.enumsService.getEnumValues('PartnerRole')
|
||||
}
|
||||
|
||||
@Get('business_role')
|
||||
async getBusinessRole() {
|
||||
return this.enumsService.getEnumValues('BusinessRole')
|
||||
}
|
||||
|
||||
@Get('provider_role')
|
||||
async getProviderRole() {
|
||||
return this.enumsService.getEnumValues('ProviderRole')
|
||||
}
|
||||
|
||||
@Get('token_type')
|
||||
async getTokenType() {
|
||||
return this.enumsService.getEnumValues('TokenType')
|
||||
}
|
||||
|
||||
@Get('unit_type')
|
||||
async getUnitType() {
|
||||
return this.enumsService.getEnumValues('UnitType')
|
||||
}
|
||||
|
||||
@Get('good_pricing_model')
|
||||
async getGoodPricingModel() {
|
||||
return this.enumsService.getEnumValues('GoodPricingModel')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,54 @@
|
||||
import translates from '@/common/constants/translates/translates'
|
||||
import {
|
||||
AccountRole,
|
||||
AccountStatus,
|
||||
BusinessRole,
|
||||
GoodPricingModel,
|
||||
LicenseStatus,
|
||||
LicenseType,
|
||||
PartnerRole,
|
||||
POSRole,
|
||||
POSStatus,
|
||||
POSType,
|
||||
ProviderRole,
|
||||
UnitType,
|
||||
UserStatus,
|
||||
UserType,
|
||||
} from '@/generated/prisma/enums'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { GoldKarat } from '../../common/enums/enums'
|
||||
import { ResponseMapper } from '../../common/response/response-mapper'
|
||||
import { AccountType, GoldKarat, TokenType } from 'common/enums/enums'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
|
||||
@Injectable()
|
||||
export class EnumsService {
|
||||
private prepareData(items: Record<string, string>) {
|
||||
console.log(translates.enums[items[0]])
|
||||
|
||||
return Object.values(items).map(item => ({
|
||||
name: translates.enums[item] || item,
|
||||
value: item,
|
||||
}))
|
||||
}
|
||||
|
||||
getAllEnums() {
|
||||
return {
|
||||
GoldKarat: Object.values(GoldKarat).filter(value => typeof value === 'string'),
|
||||
GoldKarat: this.prepareData(GoldKarat),
|
||||
UserStatus: this.prepareData(UserStatus),
|
||||
AccountRole: this.prepareData(AccountRole),
|
||||
AccountStatus: this.prepareData(AccountStatus),
|
||||
POSStatus: this.prepareData(POSStatus),
|
||||
POSRole: this.prepareData(POSRole),
|
||||
LicenseType: this.prepareData(LicenseType),
|
||||
LicenseStatus: this.prepareData(LicenseStatus),
|
||||
POSType: this.prepareData(POSType),
|
||||
UserType: this.prepareData(UserType),
|
||||
AccountType: this.prepareData(AccountType),
|
||||
PartnerRole: this.prepareData(PartnerRole),
|
||||
BusinessRole: this.prepareData(BusinessRole),
|
||||
ProviderRole: this.prepareData(ProviderRole),
|
||||
TokenType: this.prepareData(TokenType),
|
||||
UnitType: this.prepareData(UnitType),
|
||||
GoodPricingModel: this.prepareData(GoodPricingModel),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@ import { Body, Controller, Get, Param, Post } from '@nestjs/common'
|
||||
import { ConfigService } from './config.service'
|
||||
import { CreateConfigDto } from './dto/create-config.dto'
|
||||
|
||||
@Controller('config')
|
||||
@Controller('pos/config')
|
||||
export class ConfigController {
|
||||
constructor(private readonly configService: ConfigService) {}
|
||||
|
||||
@@ -6,4 +6,4 @@ import { ConfigService } from './config.service'
|
||||
controllers: [ConfigController],
|
||||
providers: [ConfigService],
|
||||
})
|
||||
export class ConfigModule {}
|
||||
export class PosConfigModule {}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { Public } from '../../common/decorators/public.decorator'
|
||||
import { ResponseMapper } from '../../common/response/response-mapper'
|
||||
import { PrismaService } from '../../prisma/prisma.service'
|
||||
import { Public } from 'common/decorators/public.decorator'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreateConfigDto } from './dto/create-config.dto'
|
||||
|
||||
@Injectable()
|
||||
@@ -10,7 +10,7 @@ export class ConfigService {
|
||||
|
||||
@Public()
|
||||
async findOne(uuid: string) {
|
||||
const config = await this.prisma.device.findUnique({
|
||||
const config = await this.prisma.userDevices.findUnique({
|
||||
where: {
|
||||
uuid,
|
||||
},
|
||||
@@ -21,7 +21,7 @@ export class ConfigService {
|
||||
|
||||
@Public()
|
||||
async create(data: CreateConfigDto) {
|
||||
const config = await this.prisma.device.upsert({
|
||||
const config = await this.prisma.userDevices.upsert({
|
||||
where: {
|
||||
uuid: data.uuid,
|
||||
},
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
import { Body, Controller, Get, Param, Post } from '@nestjs/common'
|
||||
import { CustomersService } from './customers.service'
|
||||
|
||||
@Controller('customers')
|
||||
@Controller('pos/customers')
|
||||
export class CustomersController {
|
||||
constructor(private readonly customerService: CustomersService) {}
|
||||
|
||||
+1
-1
@@ -6,4 +6,4 @@ import { CustomersService } from './customers.service'
|
||||
controllers: [CustomersController],
|
||||
providers: [CustomersService],
|
||||
})
|
||||
export class CustomersModule {}
|
||||
export class PosCustomersModule {}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { ApiProperty, PartialType } from '@nestjs/swagger'
|
||||
import { IsOptional, IsString } from 'class-validator'
|
||||
|
||||
export class CreateGoodCategoryDto {
|
||||
@IsString()
|
||||
@ApiProperty()
|
||||
name: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@ApiProperty({ required: false })
|
||||
description?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@ApiProperty({ required: false })
|
||||
image_url?: string
|
||||
}
|
||||
|
||||
export class UpdateGoodCategoryDto extends PartialType(CreateGoodCategoryDto) {}
|
||||
+2
-2
@@ -1,9 +1,9 @@
|
||||
import { Body, Controller, Get, Param, Post } from '@nestjs/common'
|
||||
import { reqTokenPayload } from '../auth/current-user.decorator'
|
||||
import { reqTokenPayload } from '../../auth/current-user.decorator'
|
||||
import { CreateGoodCategoryDto } from './dto/create-good-category.dto'
|
||||
import { GoodCategoriesService } from './good-categories.service'
|
||||
|
||||
@Controller('good_categories')
|
||||
@Controller('pos/good_categories')
|
||||
export class GoodCategoriesController {
|
||||
constructor(private readonly goodCategoriesService: GoodCategoriesService) {}
|
||||
|
||||
+1
-1
@@ -6,4 +6,4 @@ import { GoodCategoriesService } from './good-categories.service'
|
||||
controllers: [GoodCategoriesController],
|
||||
providers: [GoodCategoriesService],
|
||||
})
|
||||
export class GoodCategoriesModule {}
|
||||
export class PosGoodCategoriesModule {}
|
||||
+2
-4
@@ -1,6 +1,6 @@
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from '../../common/response/response-mapper'
|
||||
import { PrismaService } from '../../prisma/prisma.service'
|
||||
import { ResponseMapper } from '../../../common/response/response-mapper'
|
||||
import { PrismaService } from '../../../prisma/prisma.service'
|
||||
import { CreateGoodCategoryDto } from './dto/create-good-category.dto'
|
||||
|
||||
@Injectable()
|
||||
@@ -19,7 +19,6 @@ export class GoodCategoriesService {
|
||||
},
|
||||
},
|
||||
omit: {
|
||||
account_id: true,
|
||||
complex_id: true,
|
||||
},
|
||||
})
|
||||
@@ -44,7 +43,6 @@ export class GoodCategoriesService {
|
||||
data: {
|
||||
...data,
|
||||
complex_id,
|
||||
account_id: '',
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { Body, Controller, Get, Param, Post } from '@nestjs/common'
|
||||
import { reqTokenPayload } from '../auth/current-user.decorator'
|
||||
import { reqTokenPayload } from '../../auth/current-user.decorator'
|
||||
import { CreateGoodDto } from './dto/create-good.dto'
|
||||
import { GoodsService } from './goods.service'
|
||||
|
||||
@Controller('goods')
|
||||
@Controller('pos/goods')
|
||||
export class GoodsController {
|
||||
constructor(private readonly goodsService: GoodsService) {}
|
||||
|
||||
@@ -6,4 +6,4 @@ import { GoodsService } from './goods.service'
|
||||
controllers: [GoodsController],
|
||||
providers: [GoodsService],
|
||||
})
|
||||
export class GoodsModule {}
|
||||
export class PosGoodsModule {}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from '../../common/response/response-mapper'
|
||||
import { PrismaService } from '../../prisma/prisma.service'
|
||||
import { ResponseMapper } from '../../../common/response/response-mapper'
|
||||
import { PrismaService } from '../../../prisma/prisma.service'
|
||||
import { CreateGoodDto } from './dto/create-good.dto'
|
||||
|
||||
@Injectable()
|
||||
@@ -13,7 +13,6 @@ export class GoodsService {
|
||||
complex_id,
|
||||
},
|
||||
omit: {
|
||||
account_id: true,
|
||||
complex_id: true,
|
||||
deleted_at: true,
|
||||
},
|
||||
@@ -30,7 +29,6 @@ export class GoodsService {
|
||||
},
|
||||
|
||||
omit: {
|
||||
account_id: true,
|
||||
complex_id: true,
|
||||
deleted_at: true,
|
||||
},
|
||||
@@ -45,18 +43,28 @@ export class GoodsService {
|
||||
const dataToCreate = {
|
||||
...rest,
|
||||
complex_id,
|
||||
account_id: '',
|
||||
category: {
|
||||
connect: {
|
||||
connect: {
|
||||
category: {
|
||||
id: category_id,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
const good = await this.prisma.good.create({
|
||||
data: dataToCreate,
|
||||
data: {
|
||||
...rest,
|
||||
category: {
|
||||
connect: {
|
||||
id: category_id,
|
||||
},
|
||||
},
|
||||
complex: {
|
||||
connect: {
|
||||
id: complex_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
omit: {
|
||||
account_id: true,
|
||||
complex_id: true,
|
||||
deleted_at: true,
|
||||
},
|
||||
@@ -0,0 +1,24 @@
|
||||
import { Injectable, NestMiddleware, UnauthorizedException } from '@nestjs/common'
|
||||
import { NextFunction, Request, Response } from 'express'
|
||||
|
||||
@Injectable()
|
||||
export class PosMiddleware implements NestMiddleware {
|
||||
use(req: Request, _res: Response, next: NextFunction) {
|
||||
console.log('first')
|
||||
// @ts-ignore
|
||||
console.log(req.dataPayload)
|
||||
// const a = reqTokenPayload()
|
||||
// console.log(a)
|
||||
|
||||
// Middleware-based admin check (replace with real auth logic)
|
||||
const isAdminHeader = req.headers['x-admin']
|
||||
|
||||
if (isAdminHeader === 'true') {
|
||||
// mark request if needed downstream
|
||||
req['isAdminRequest'] = true
|
||||
}
|
||||
return next()
|
||||
|
||||
throw new UnauthorizedException('Admin access required')
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import { MiddlewareConsumer, Module, NestModule, RequestMethod } from '@nestjs/common'
|
||||
import { PosConfigModule } from './config/config.module'
|
||||
import { PosCustomersModule } from './customers/customers.module'
|
||||
import { PosGoodCategoriesModule } from './good-categories/good-categories.module'
|
||||
import { PosGoodsModule } from './goods/goods.module'
|
||||
import { PosMiddleware } from './pos.middleware'
|
||||
import { PosSalesInvoicesModule } from './sales-invoices/sales-invoices.module'
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
PosConfigModule,
|
||||
PosCustomersModule,
|
||||
PosGoodCategoriesModule,
|
||||
PosGoodsModule,
|
||||
PosSalesInvoicesModule,
|
||||
],
|
||||
})
|
||||
export class PosModule implements NestModule {
|
||||
configure(consumer: MiddlewareConsumer) {
|
||||
// apply middleware to all routes within this module (scoped under /admin)
|
||||
consumer.apply(PosMiddleware).forRoutes({
|
||||
path: '/pos*path',
|
||||
method: RequestMethod.ALL,
|
||||
version: '1',
|
||||
})
|
||||
}
|
||||
}
|
||||
+7
-4
@@ -8,9 +8,9 @@ import {
|
||||
IsOptional,
|
||||
IsString,
|
||||
} from 'class-validator'
|
||||
import type { CustomerIndividual, CustomerLegal } from '../../../generated/prisma/client'
|
||||
import { CustomerType } from '../../../generated/prisma/enums'
|
||||
import { CreateSalesInvoiceItemDto } from '../../sales-invoice-items/dto/create-sales-invoice-item.dto'
|
||||
import type { CustomerIndividual, CustomerLegal } from 'generated/prisma/client'
|
||||
import { CustomerType } from 'generated/prisma/enums'
|
||||
import { CreateSalesInvoiceItemDto } from '../sales-invoice-items/dto/create-sales-invoice-item.dto'
|
||||
|
||||
export class CreateSalesInvoiceDto {
|
||||
@IsNumber()
|
||||
@@ -18,7 +18,10 @@ export class CreateSalesInvoiceDto {
|
||||
total_amount: number
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsDateString({ strict: true }, { message: 'invoice_date must be a valid ISO-8601 string' })
|
||||
@IsDateString(
|
||||
{ strict: true },
|
||||
{ message: 'invoice_date must be a valid ISO-8601 string' },
|
||||
)
|
||||
invoice_date: Date
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
+6
-6
@@ -1,7 +1,7 @@
|
||||
import { ApiProperty, PartialType } from '@nestjs/swagger'
|
||||
import { IsEnum, IsNumber, IsObject, IsOptional, IsString } from 'class-validator'
|
||||
import type { SaleInvoiceStandardPayload } from '../../../common/interfaces/sale-invoice-payload'
|
||||
import { SalesInvoiceItemPricingModel, UnitType } from '../../../generated/prisma/enums'
|
||||
import type { SaleInvoiceStandardPayload } from 'common/interfaces/sale-invoice-payload'
|
||||
import { UnitType } from 'generated/prisma/enums'
|
||||
|
||||
export class CreateSalesInvoiceItemDto {
|
||||
@IsNumber()
|
||||
@@ -34,10 +34,10 @@ export class CreateSalesInvoiceItemDto {
|
||||
@ApiProperty({ enum: Object.values(UnitType) })
|
||||
unit_type: UnitType
|
||||
|
||||
@IsEnum(SalesInvoiceItemPricingModel)
|
||||
@ApiProperty({ enum: Object.values(SalesInvoiceItemPricingModel) })
|
||||
@IsOptional()
|
||||
pricingModel: SalesInvoiceItemPricingModel
|
||||
// @IsEnum(SalesInvoiceItemPricingModel)
|
||||
// @ApiProperty({ enum: Object.values(SalesInvoiceItemPricingModel) })
|
||||
// @IsOptional()
|
||||
// pricingModel: SalesInvoiceItemPricingModel
|
||||
|
||||
@IsOptional()
|
||||
@IsObject()
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user