Files
tripz-llc/backend/src/modules/users/users.service.ts
T
Hamza-AyedandClaude Opus 4.8 9d6b752ea8 feat: المجموعة A (زمن حقيقي + FCM + Redis) + إصلاح سباق المحفظة
المجموعة A (docs/17):
- A1: FCM على كل انتقال حالة (priority high) + حذف التوكنات الميتة
- A2: common/i18n (ar/en) + عمود users.language — الإشعارات بلغة المستخدم
- A3: TripStateService — حالة الرحلة الجارية في Redis hash (TTL 6س)؛
  الانتقال صار UPDATE شرطي + قراءة واحدة بدل ~5 استعلامات
- A4: قبول ذرّي — CAS بـLua في Redis + UPDATE ... WHERE status='searching'
  كحَكَم نهائي؛ أول سائق يفوز والباقي يُرفضون بلا لمس القاعدة
- A5: مجموعة العروض في Redis + بث trip:offer_taken و FCM لبقية السائقين
- A6: GET /trips/available — السائق يسحب الطلبات القريبة
- A7: FCM data-only بحمولة كاملة للـoverlay

I1 — إصلاح سباق المحفظة (ثغرة مالية):
- credit/debit كانا read-modify-write على balance بلا قفل → خصمان متزامنان
  يكتبان فوق بعضهما. صارا UPDATE ذرّي واحد بشرط balance >= :amount،
  والقيد+الرصيد في معاملة واحدة
- wallet_txns.balance_after للتدقيق + CHECK (balance >= 0) كشبكة أمان
- إنشاء المحفظة عبر ON CONFLICT DO NOTHING (سباق ثانٍ كان كامناً)

الاختبارات تعمل على السيرفر (docs/15):
- npm test صار جزءاً من مرحلة builder — فشل اختبار = فشل بناء = لا نشر
- pg-mem + ioredis-mock: بلا شبكة وبلا قاعدة حقيقية
- scripts/wallet-race-test.mjs للتزامن الحقيقي على السيرفر

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-17 02:23:00 +03:00

65 lines
2.3 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User, UserRole } from './entities/user.entity';
@Injectable()
export class UsersService {
constructor(
@InjectRepository(User)
private readonly userRepository: Repository<User>,
) {}
async findByPhone(tenantId: string, phone: string): Promise<User | null> {
return this.userRepository.findOne({ where: { tenant_id: tenantId, phone } });
}
async findById(tenantId: string, id: string): Promise<User | null> {
return this.userRepository.findOne({ where: { tenant_id: tenantId, id } });
}
async create(tenantId: string, phone: string, role: string = 'rider'): Promise<User> {
const user = this.userRepository.create({ tenant_id: tenantId, phone, role: role as UserRole });
return this.userRepository.save(user);
}
async findOrCreate(tenantId: string, phone: string, role: string = 'rider'): Promise<User> {
const existing = await this.findByPhone(tenantId, phone);
return existing ?? this.create(tenantId, phone, role);
}
async listByTenant(tenantId: string): Promise<User[]> {
return this.userRepository.find({ where: { tenant_id: tenantId } });
}
async updateProfile(
tenantId: string,
id: string,
data: Partial<Pick<User, 'name' | 'language'>>,
): Promise<User | null> {
const patch: Partial<User> = {};
if (data.name !== undefined) patch.name = data.name;
if (data.language !== undefined) patch.language = data.language;
if (Object.keys(patch).length > 0) {
await this.userRepository.update({ tenant_id: tenantId, id }, patch);
}
return this.findById(tenantId, id);
}
/** لغة الإشعارات للمستخدم — استعلام خفيف (عمود واحد) يُستدعى قبل كل push. */
async getLanguage(tenantId: string, id: string): Promise<string | null> {
const row = await this.userRepository.findOne({
where: { tenant_id: tenantId, id },
select: { language: true },
});
return row?.language ?? null;
}
async setRole(tenantId: string, id: string, role: string): Promise<void> {
await this.userRepository.update(
{ tenant_id: tenantId, id },
{ role: role as UserRole },
);
}
}