update: add slug field to the danak service entity

This commit is contained in:
mahyargdz
2025-05-04 11:31:53 +03:30
parent 721968ed2e
commit 8fe1d398a6
9 changed files with 55 additions and 24 deletions
@@ -103,4 +103,11 @@ export class CreateServiceDto {
@IsUrl({ protocols: ["http", "https"], require_protocol: true }, { message: ServiceMessage.COVER_URL_SHOULD_BE_URL })
@ApiProperty({ description: "The cover url of the service", example: "https://example.com/cover.png" })
coverUrl?: string;
@IsOptional()
@IsNotEmpty({ message: ServiceMessage.SLUG_REQUIRED })
@IsString({ message: ServiceMessage.SLUG_STRING })
@Length(3, 150, { message: ServiceMessage.SLUG_LENGTH })
@ApiProperty({ description: "The slug of the service", example: "service-slug" })
slug?: string;
}
@@ -1,4 +1,4 @@
import { Column, DeleteDateColumn, Entity, ManyToOne, OneToMany } from "typeorm";
import { Column, DeleteDateColumn, Entity, Index, ManyToOne, OneToMany } from "typeorm";
import { DanakServiceCategory } from "./danak-service-category.entity";
import { DanakServiceImage } from "./danak-service-image.entity";
@@ -13,10 +13,15 @@ import { ServicesLanguage } from "../enums/services-language.enum";
@Entity()
export class DanakService extends BaseEntity {
@Column({ type: "varchar", length: 150, nullable: false, unique: true })
@Index()
name: string;
@Column({ type: "varchar", length: 150, nullable: true })
title: string;
title: string | null;
@Column({ type: "varchar", length: 150, nullable: true, unique: true })
@Index()
slug: string | null;
@Column({ type: "boolean", nullable: false, default: false })
isDanakSuggest: boolean;
@@ -55,7 +60,7 @@ export class DanakService extends BaseEntity {
icon: string;
@Column({ type: "varchar", length: 255, nullable: true })
coverUrl: string;
coverUrl: string | null;
//-------------------------------------
@ManyToOne(() => DanakServiceCategory, (danakServiceCategory) => danakServiceCategory.danakServices, {
nullable: false,
@@ -271,11 +271,13 @@ export class DanakServicesService {
const existService = await this.danakServicesRepository.findOneByName(createDto.name);
if (existService) throw new BadRequestException(ServiceMessage.NAME_EXIST);
const slug = this.generateSlug(createDto.slug ?? createDto.title);
const images = createDto.images.map((image) => {
return { imageUrl: image };
});
const service = this.danakServicesRepository.create({ ...createDto, category, images });
const service = this.danakServicesRepository.create({ ...createDto, category, images, slug });
await this.danakServicesRepository.save(service);
return {
message: CommonMessage.CREATED,
@@ -314,7 +316,17 @@ export class DanakServicesService {
images = newImages;
}
await this.danakServicesRepository.save({ ...service, ...updateDto, images: images.length ? images : service.images });
if (updateDto.slug) {
const existSlug = await this.danakServicesRepository.findOne({ where: { slug: updateDto.slug, id: Not(serviceId) } });
if (existSlug) throw new BadRequestException(ServiceMessage.SERVICE_ALREADY_EXISTS_WITH_THIS_SLUG);
}
await this.danakServicesRepository.save({
...service,
...updateDto,
slug: this.generateSlug(updateDto.slug ?? service.slug ?? service.name),
images: images.length ? images : service.images,
});
return {
message: CommonMessage.UPDATE_SUCCESS,
service,
@@ -555,6 +567,18 @@ export class DanakServicesService {
}
/******************************************** */
private generateSlug(slug: string): string {
if (!slug) return "";
// Replace spaces and special characters with hyphens, preserve Farsi characters
return slug
.trim()
.replace(/[&\\#,+()$~%.'":*?<>{}]/g, "") // Remove special characters
.replace(/\s+/g, "-") // Replace spaces with hyphens
.replace(/-+/g, "-") // Replace multiple hyphens with single hyphen
.trim();
}
private async checkUserPurchasedService(userId: string, serviceId: string) {
const subscription = await this.danakServicesRepository
.createQueryBuilder()