main.js

 avatar
unknown
javascript
2 years ago
1.3 kB
6
Indexable
async getDiscountInfo(payload) {
        try {
            this.logger.setContext('getDiscount');
            this.logger.info({
                message: 'Start executing method',
                data: { payload },
            });

            payload.media = sanitizePhoneNumber(payload.media);
            this.logger.info({
                message: 'Phone number after sanitized',
                data: { media: payload.media },
            });

            if (!validatePhoneNumber(payload.media)) {
                this.logger.info('Phone number caught in validation');
                throw new AppError('Invalid phone number, please provide a valid phone number');
            }

            const discountInfo = await this.loyaltyDiscountRepo.getDiscountInfo(payload);

            if (!discountInfo || discountInfo.discountLevel <= 0) {
                throw new AppError(ORDER_ERROR_MESSAGE.noDiscountFound);
            }

            return this.prepareDiscountInfo(discountInfo);
        } catch (error) {
            if (error.message.includes(ORDER_ERROR_MESSAGE.noDiscountFound)) {
                this.logger.warn({
                    message: ORDER_ERROR_MESSAGE.noDiscountFound,
                });
            } else {
                this.logger.error({ error });
            }
            throw error;
        }
    }
Editor is loading...