customers
This commit is contained in:
@@ -3,6 +3,7 @@ import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { AuthUser } from '../../auth/interfaces/auth-user.interface';
|
||||
import { Customer } from '../../customers/entities/customer.entity';
|
||||
import { SalonCustomer } from '../../customers/entities/salon-customer.entity';
|
||||
import { Reserve } from '../../reserves/entities/reserve.entity';
|
||||
import { UserRole } from '../../users/enums/user-role.enum';
|
||||
import { AuthorizationService } from '../authorization.service';
|
||||
@@ -13,6 +14,8 @@ export class CustomerPolicy {
|
||||
private readonly authorizationService: AuthorizationService,
|
||||
@InjectRepository(Reserve)
|
||||
private readonly reservesRepository: Repository<Reserve>,
|
||||
@InjectRepository(SalonCustomer)
|
||||
private readonly salonCustomersRepository: Repository<SalonCustomer>,
|
||||
) {}
|
||||
|
||||
async canRead(user: AuthUser, customer: Customer): Promise<boolean> {
|
||||
@@ -23,7 +26,10 @@ export class CustomerPolicy {
|
||||
return customer.userId === user.id;
|
||||
}
|
||||
if (user.role === UserRole.SALON) {
|
||||
return this.hasReserveAtSalon(customer.id, user.id);
|
||||
return (
|
||||
(await this.hasReserveAtSalon(customer.id, user.id)) ||
|
||||
(await this.hasLinkAtSalon(customer.id, user.id))
|
||||
);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -49,4 +55,16 @@ export class CustomerPolicy {
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
private async hasLinkAtSalon(
|
||||
customerId: string,
|
||||
salonOwnerId: string,
|
||||
): Promise<boolean> {
|
||||
return this.salonCustomersRepository.exists({
|
||||
where: {
|
||||
customerId,
|
||||
salon: { ownerId: salonOwnerId },
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user