Refactor addOrFindSchoolCompanyDetail to normalize school/company names and improve search logic
This commit is contained in:
@@ -32,6 +32,9 @@ import config from '@/config/config';
|
||||
// return R * c;
|
||||
// }
|
||||
|
||||
const normalizeName = (name: string) =>
|
||||
name.trim().toLowerCase().replace(/\s+/g, " ");
|
||||
|
||||
const attachPresignedUrl = async (file: string | null | undefined) => {
|
||||
if (!file) return null;
|
||||
|
||||
@@ -1732,6 +1735,7 @@ export class UserService {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
async searchActivities(
|
||||
userId: number,
|
||||
searchCriteria: {
|
||||
@@ -1922,15 +1926,14 @@ export class UserService {
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add or Find School/Company Detail
|
||||
* If school/company with same name exists in same city, return existing record
|
||||
* Otherwise, create a new record
|
||||
*/
|
||||
|
||||
|
||||
async addOrFindSchoolCompanyDetail(dto: AddSchoolCompanyDetailDTO) {
|
||||
const { schoolCompanyName, isSchool, cityXid } = dto;
|
||||
|
||||
// Verify that the city exists
|
||||
|
||||
const normalizedName = normalizeName(schoolCompanyName);
|
||||
|
||||
// ✅ 1. Verify city exists
|
||||
const cityExists = await this.prisma.cities.findFirst({
|
||||
where: {
|
||||
id: cityXid,
|
||||
@@ -1938,45 +1941,43 @@ export class UserService {
|
||||
deletedAt: null,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
if (!cityExists) {
|
||||
throw new ApiError(404, 'City not found');
|
||||
throw new ApiError(404, "City not found");
|
||||
}
|
||||
|
||||
// Check if school/company with same name already exists in this city
|
||||
const existingSchoolCompany = await this.prisma.schoolCompany.findFirst({
|
||||
|
||||
// ✅ 2. Check existing (lowercase match)
|
||||
const existing = await this.prisma.schoolCompany.findFirst({
|
||||
where: {
|
||||
schoolCompanyName: {
|
||||
equals: schoolCompanyName,
|
||||
mode: 'insensitive',
|
||||
},
|
||||
cityXid: cityXid,
|
||||
isSchool: isSchool,
|
||||
schoolCompanyName: normalizedName,
|
||||
cityXid,
|
||||
isSchool,
|
||||
isActive: true,
|
||||
deletedAt: null,
|
||||
},
|
||||
});
|
||||
|
||||
if (existingSchoolCompany) {
|
||||
// Return existing record
|
||||
|
||||
if (existing) {
|
||||
return {
|
||||
isNew: false,
|
||||
data: existingSchoolCompany,
|
||||
data: existing,
|
||||
message: "Already exists",
|
||||
};
|
||||
}
|
||||
|
||||
// Create new school/company record
|
||||
const newSchoolCompany = await this.prisma.schoolCompany.create({
|
||||
|
||||
// ✅ 3. Create new (store lowercase)
|
||||
const created = await this.prisma.schoolCompany.create({
|
||||
data: {
|
||||
schoolCompanyName: schoolCompanyName,
|
||||
isSchool: isSchool,
|
||||
cityXid : cityXid
|
||||
schoolCompanyName: normalizedName,
|
||||
isSchool,
|
||||
cityXid,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
return {
|
||||
isNew: true,
|
||||
data: newSchoolCompany,
|
||||
data: created,
|
||||
message: "Created successfully",
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user