-
Notifications
You must be signed in to change notification settings - Fork 1
/
Customer.service.ts
31 lines (28 loc) · 1.3 KB
/
Customer.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { inject, injectable } from "tsyringe";
import { CustomerRepositoryI } from "./repository/CustomerRepository.contract";
import { BaseLogger } from "@/interfaces";
import { comparePassword, createToken } from "@/helpers";
import { TOKENS } from '@/di/Tokens';
@injectable()
export default class CustomerService {
constructor(@inject(TOKENS.customerRepository) private customerRepository: CustomerRepositoryI, @inject(TOKENS.logger) private logger: BaseLogger) {
this.customerRepository = customerRepository;
this.logger = logger;
}
public async login(username: string, password: string): Promise<any> {
this.logger.info(`CustomerService :: login :: ${username}`);
const customer = await this.customerRepository.findCustomer(username);
if (!customer) {
this.logger.error(`CustomerService :: login :: 401 :: ${username}`);
return [false, null];
}
const passwordIsSame = await comparePassword(password, customer.password);
if (!passwordIsSame) {
this.logger.error(`CustomerService :: login :: invalid password ::`);
return [false, null];
}
delete customer.password;
const token = createToken({ ...customer, role: 'customer' });
return [true, { ...customer, token }];
}
}