From 048abb40dd092aa53993bf06347b6737afba9107 Mon Sep 17 00:00:00 2001 From: Mahyargdz Date: Wed, 21 May 2025 16:37:56 +0330 Subject: [PATCH] chore: add business update --- src/common/enums/message.enum.ts | 8 ++++++ .../DTO/update-business-settings.dto.ts | 25 ++++++++++++++++++ .../businesses/businesses.controller.ts | 19 +++++++++++++- .../businesses/services/businesses.service.ts | 26 ++++++++++++++++++- 4 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 src/modules/businesses/DTO/update-business-settings.dto.ts diff --git a/src/common/enums/message.enum.ts b/src/common/enums/message.enum.ts index 6bda8aa..47689ca 100755 --- a/src/common/enums/message.enum.ts +++ b/src/common/enums/message.enum.ts @@ -604,4 +604,12 @@ export const enum BusinessMessage { DOMAIN_REQUIRED = "دامنه مورد نیاز است", DOMAIN_MUST_BE_STRING = "دامنه باید یک رشته باشد", STAFF_NOT_FOUND = "کاربر ادمین یافت نشد", + ZARINPAL_MERCHANT_ID_REQUIRED = "شناسه مرچنت زرین پال مورد نیاز است", + ZARINPAL_MERCHANT_ID_STRING = "شناسه مرچنت زرین پال باید یک رشته باشد", + LOGO_URL_REQUIRED = "آدرس تصویر لوگو مورد نیاز است", + LOGO_URL_STRING = "آدرس تصویر لوگو باید یک رشته باشد", + LOGO_URL_INVALID = "آدرس تصویر لوگو معتبر نیست", + BUSINESS_SETTINGS_UPDATED = "تنظیمات کسب و کار با موفقیت به روز رسانی شد", + NAME_REQUIRED = "نام کسب و کار مورد نیاز است", + NAME_STRING = "نام کسب و کار باید یک رشته باشد", } diff --git a/src/modules/businesses/DTO/update-business-settings.dto.ts b/src/modules/businesses/DTO/update-business-settings.dto.ts new file mode 100644 index 0000000..fd3181e --- /dev/null +++ b/src/modules/businesses/DTO/update-business-settings.dto.ts @@ -0,0 +1,25 @@ +import { ApiProperty } from "@nestjs/swagger"; +import { IsNotEmpty, IsOptional, IsString, IsUrl } from "class-validator"; + +import { BusinessMessage } from "../../../common/enums/message.enum"; + +export class UpdateBusinessSettingsDto { + @IsOptional() + @IsNotEmpty({ message: BusinessMessage.ZARINPAL_MERCHANT_ID_REQUIRED }) + @IsString({ message: BusinessMessage.ZARINPAL_MERCHANT_ID_STRING }) + @ApiProperty({ description: "Zarinpal merchant id", example: "1234567890" }) + zarinpalMerchantId?: string; + + @IsOptional() + @IsNotEmpty({ message: BusinessMessage.LOGO_URL_REQUIRED }) + @IsString({ message: BusinessMessage.LOGO_URL_STRING }) + @IsUrl({ protocols: ["https"] }, { message: BusinessMessage.LOGO_URL_INVALID }) + @ApiProperty({ description: "Logo url", example: "https://example.com/logo.png" }) + logoUrl?: string; + + // @IsOptional() + // @IsNotEmpty({ message: BusinessMessage.NAME_REQUIRED }) + // @IsString({ message: BusinessMessage.NAME_STRING }) + // @ApiProperty({ description: "Name", example: "Example" }) + // name?: string; +} diff --git a/src/modules/businesses/businesses.controller.ts b/src/modules/businesses/businesses.controller.ts index 7dbb72d..d369908 100644 --- a/src/modules/businesses/businesses.controller.ts +++ b/src/modules/businesses/businesses.controller.ts @@ -1,16 +1,19 @@ -import { Body, Controller, Get, Param, Post } from "@nestjs/common"; +import { Body, Controller, Get, Param, Patch, Post, UseInterceptors } from "@nestjs/common"; import { ApiHeader, ApiOperation } from "@nestjs/swagger"; import { UpdateBusinessDomainDto } from "./DTO/business-domain.dto"; import { BusinessSlugParamDto } from "./DTO/business-slug-param.dto"; +import { UpdateBusinessSettingsDto } from "./DTO/update-business-settings.dto"; import { Business } from "./entities/business.entity"; import { BusinessesService } from "./services/businesses.service"; import { DomainVerificationService } from "./services/domain-verification.service"; import { AuthGuards } from "../../common/decorators/auth-guard.decorator"; import { BusinessDec } from "../../common/decorators/business.decorator"; +import { BusinessInterceptor } from "../../core/interceptors/business.interceptor"; @Controller("business") @ApiHeader({ name: "x-business-id", description: "Business ID" }) +@UseInterceptors(BusinessInterceptor) export class BusinessesController { constructor( private readonly businessesService: BusinessesService, @@ -23,6 +26,20 @@ export class BusinessesController { return this.businessesService.getBusinessBySlug(params.slug); } + @Patch("settings") + @ApiOperation({ summary: "Update business settings" }) + @AuthGuards() + async updateBusinessSettings(@BusinessDec("id") businessId: string, @Body() settingsDto: UpdateBusinessSettingsDto) { + return this.businessesService.updateBusinessSettings(businessId, settingsDto); + } + + @Get("settings") + @ApiOperation({ summary: "Get business settings" }) + @AuthGuards() + async getBusinessSettings(@BusinessDec("id") businessId: string) { + return this.businessesService.getBusinessSettings(businessId); + } + @Post("domain") @ApiOperation({ summary: "Update business domain" }) @AuthGuards() diff --git a/src/modules/businesses/services/businesses.service.ts b/src/modules/businesses/services/businesses.service.ts index 3b4ffa0..1446f65 100644 --- a/src/modules/businesses/services/businesses.service.ts +++ b/src/modules/businesses/services/businesses.service.ts @@ -2,6 +2,7 @@ import { EntityManager } from "@mikro-orm/postgresql"; import { BadRequestException, Injectable } from "@nestjs/common"; import { BusinessMessage } from "../../../common/enums/message.enum"; +import { UpdateBusinessSettingsDto } from "../DTO/update-business-settings.dto"; import { BusinessStaff } from "../entities/business-staff.entity"; import { BusinessRepository } from "../repositories/business.repository"; @@ -18,6 +19,7 @@ export class BusinessesService { return business; } + //************************************************** */ async getBusinessBySlug(slug: string) { const business = await this.businessRepository.findOne({ slug, deletedAt: null }); @@ -25,6 +27,28 @@ export class BusinessesService { return { business }; } + //************************************************** */ + async updateBusinessSettings(businessId: string, settingsDto: UpdateBusinessSettingsDto) { + const business = await this.businessRepository.findOne({ id: businessId, deletedAt: null }); + if (!business) throw new BadRequestException(BusinessMessage.NOT_FOUND); + + this.businessRepository.assign(business, settingsDto); + await this.em.flush(); + + return { + message: BusinessMessage.BUSINESS_SETTINGS_UPDATED, + business, + }; + } + //************************************************** */ + + async getBusinessSettings(businessId: string) { + const business = await this.businessRepository.findOne({ id: businessId, deletedAt: null }); + if (!business) throw new BadRequestException(BusinessMessage.NOT_FOUND); + + return { business }; + } + //************************************************** */ async getBusinessById(id: string) { const business = await this.businessRepository.findOne({ id, deletedAt: null }); @@ -33,7 +57,7 @@ export class BusinessesService { return business; } - /*******************************/ + //************************************************** */ async findAdminWithLeastTickets() { // First query to get the ID of the business staff with the least tickets