mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +00:00
35 lines
984 B
TypeScript
35 lines
984 B
TypeScript
import { ServiceError } from '@/exceptions';
|
|
import parsePhoneNumber from 'libphonenumber-js';
|
|
import { Service } from 'typedi';
|
|
|
|
const ERRORS = {
|
|
CUSTOMER_HAS_NO_PHONE_NUMBER: 'CUSTOMER_HAS_NO_PHONE_NUMBER',
|
|
CUSTOMER_SMS_NOTIFY_PHONE_INVALID: 'CUSTOMER_SMS_NOTIFY_PHONE_INVALID',
|
|
};
|
|
|
|
@Service()
|
|
export default class SaleNotifyBySms {
|
|
/**
|
|
* Validate the customer phone number.
|
|
* @param {ICustomer} customer
|
|
*/
|
|
public validateCustomerPhoneNumber = (personalPhone: string) => {
|
|
if (!personalPhone) {
|
|
throw new ServiceError(ERRORS.CUSTOMER_HAS_NO_PHONE_NUMBER);
|
|
}
|
|
this.validateCustomerPhoneNumberLocally(personalPhone);
|
|
};
|
|
|
|
/**
|
|
*
|
|
* @param {string} personalPhone
|
|
*/
|
|
public validateCustomerPhoneNumberLocally = (personalPhone: string) => {
|
|
const phoneNumber = parsePhoneNumber(personalPhone, 'LY');
|
|
|
|
if (!phoneNumber || !phoneNumber.isValid()) {
|
|
throw new ServiceError(ERRORS.CUSTOMER_SMS_NOTIFY_PHONE_INVALID);
|
|
}
|
|
};
|
|
}
|