slugify bug fixed
This commit is contained in:
@@ -38,23 +38,12 @@ export class RestaurantsService {
|
|||||||
async setupRestuarant(dto: CreateRestaurantDto): Promise<Restaurant> {
|
async setupRestuarant(dto: CreateRestaurantDto): Promise<Restaurant> {
|
||||||
return await this.em.transactional(async (em) => {
|
return await this.em.transactional(async (em) => {
|
||||||
// Generate or validate slug
|
// Generate or validate slug
|
||||||
let slug = dto.slug;
|
const slug = slugify(dto.slug ?? dto.name, {
|
||||||
if (!slug) {
|
lower: true,
|
||||||
// Generate slug from restaurant name
|
strict: true,
|
||||||
slug = slugify(dto.name, {
|
locale: 'fa'
|
||||||
lower: true,
|
});
|
||||||
strict: true,
|
|
||||||
locale: 'fa'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure slug uniqueness
|
|
||||||
let finalSlug = slug;
|
|
||||||
let counter = 1;
|
|
||||||
while (await em.findOne(Restaurant, { slug: finalSlug })) {
|
|
||||||
finalSlug = `${slug}-${counter}`;
|
|
||||||
counter++;
|
|
||||||
}
|
|
||||||
|
|
||||||
const validateSubscriptionId = await em.findOne(Restaurant, { subscriptionId: dto.subscriptionId })
|
const validateSubscriptionId = await em.findOne(Restaurant, { subscriptionId: dto.subscriptionId })
|
||||||
if (validateSubscriptionId) {
|
if (validateSubscriptionId) {
|
||||||
@@ -64,12 +53,12 @@ export class RestaurantsService {
|
|||||||
// Create restaurant
|
// Create restaurant
|
||||||
const restaurant = em.create(Restaurant, {
|
const restaurant = em.create(Restaurant, {
|
||||||
name: dto.name,
|
name: dto.name,
|
||||||
slug: finalSlug,
|
slug,
|
||||||
establishedYear: dto.establishedYear,
|
establishedYear: dto.establishedYear,
|
||||||
phone: dto.phone,
|
phone: dto.phone,
|
||||||
isActive: true,
|
isActive: true,
|
||||||
plan: dto.plan,
|
plan: dto.plan,
|
||||||
domain: `https://dmenu-plus-front.dev.danakcorp.com/${finalSlug}`,
|
domain: `https://dmenu-plus-front.dev.danakcorp.com/${slug}`,
|
||||||
subscriptionId: dto.subscriptionId,
|
subscriptionId: dto.subscriptionId,
|
||||||
subscriptionEndDate: dto.subscriptionEndDate,
|
subscriptionEndDate: dto.subscriptionEndDate,
|
||||||
subscriptionStartDate: dto.subscriptionStartDate,
|
subscriptionStartDate: dto.subscriptionStartDate,
|
||||||
@@ -169,30 +158,14 @@ export class RestaurantsService {
|
|||||||
throw new NotFoundException(RestMessage.NOT_FOUND);
|
throw new NotFoundException(RestMessage.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle slug generation/update
|
if(dto.slug){
|
||||||
if (dto.slug !== undefined || dto.name !== undefined) {
|
dto.slug = slugify(dto.slug, {
|
||||||
let slug = dto.slug;
|
lower: true,
|
||||||
|
strict: true,
|
||||||
|
locale: 'fa'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (!slug && dto.name) {
|
|
||||||
// Generate slug from restaurant name if slug not provided but name is
|
|
||||||
slug = slugify(dto.name, {
|
|
||||||
lower: true,
|
|
||||||
strict: true,
|
|
||||||
locale: 'fa'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure slug uniqueness if slug is being updated
|
|
||||||
if (slug) {
|
|
||||||
let finalSlug = slug;
|
|
||||||
let counter = 1;
|
|
||||||
while (await this.em.findOne(Restaurant, { slug: finalSlug, id: { $ne: id } })) {
|
|
||||||
finalSlug = `${slug}-${counter}`;
|
|
||||||
counter++;
|
|
||||||
}
|
|
||||||
dto.slug = finalSlug;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.restRepository.assign(restaurant, dto);
|
this.restRepository.assign(restaurant, dto);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user