optimize performance
deploy to danak / build_and_deploy (push) Has been cancelled

This commit is contained in:
2026-06-18 23:01:44 +03:30
parent 2ce395d1f8
commit 65a34b97e2
+46 -11
View File
@@ -445,8 +445,9 @@ export class UserService {
errors: [],
};
const validRows: Array<{ row: (typeof rows)[number]; phone: string }> = [];
for (const row of rows) {
try {
if (!row.phone) {
result.errors.push({
row: row.rowNumber,
@@ -465,11 +466,23 @@ export class UserService {
continue;
}
let user = await this.userRepository.findOne({ phone: normalizedPhone });
validRows.push({ row, phone: normalizedPhone });
}
if (validRows.length === 0) {
return result;
}
const phones = [...new Set(validRows.map(entry => entry.phone))];
const existingUsers = await this.userRepository.find({ phone: { $in: phones } });
const userByPhone = new Map(existingUsers.map(user => [user.phone, user]));
for (const { row, phone } of validRows) {
let user = userByPhone.get(phone);
if (!user) {
const createData = {
phone: normalizedPhone,
phone,
firstName: row.firstName?.trim() || '[نام]',
lastName: row.lastName?.trim() || undefined,
gender: row.gender,
@@ -478,7 +491,7 @@ export class UserService {
} as unknown as RequiredEntityData<User>;
user = this.userRepository.create(createData);
await this.em.persistAndFlush(user);
userByPhone.set(phone, user);
result.usersCreated++;
} else {
const updates: Partial<User> = {};
@@ -493,26 +506,48 @@ export class UserService {
}
if (Object.keys(updates).length > 0) {
this.em.assign(user, updates);
await this.em.flush();
}
}
}
const existingLink = await this.userRestaurantRepository.findOne({ user, restaurant });
if (existingLink) {
const userIds = [...userByPhone.values()].map(user => user.id);
const existingLinks = await this.userRestaurantRepository.find(
{ restaurant, user: { $in: userIds } },
{ populate: ['user'] },
);
const linkedUserIds = new Set(existingLinks.map(link => link.user.id));
const linkedPhonesInBatch = new Set<string>();
for (const { phone } of validRows) {
const user = userByPhone.get(phone);
if (!user) {
continue;
}
if (linkedUserIds.has(user.id) || linkedPhonesInBatch.has(phone)) {
result.usersAlreadyLinked++;
continue;
}
await this.addUserToRestaurant(user, restaurant);
this.userRestaurantRepository.create({
user,
restaurant,
orderCount: 0,
totalOrderAmount: 0,
});
linkedUserIds.add(user.id);
linkedPhonesInBatch.add(phone);
result.usersLinked++;
}
try {
await this.em.flush();
} catch (error) {
result.errors.push({
row: row.rowNumber,
phone: row.phone,
row: 0,
message: error instanceof Error ? error.message : 'Unknown error',
});
}
}
return result;
}