import { Controller, Post, Body, Param, Get } from '@nestjs/common' import { plainToClass } from 'class-transformer' import { RequiredArgumentError } from '../../../errors' import { Patient } from '../../../models' import { isBlank, isEmail, keepNumbers } from '../../../utils' import { ConverterPage } from '../domain' import { AddSubscriptionToStudent, AddExternalSubscriptionToPatient } from './params' @Controller('converter-page') export class ConverterPageController { constructor(private readonly converterPage: ConverterPage) {} @Get('is-student/:login') async isStudent(@Param('login') login: string): Promise { const sanitizedLogin = this.getSanitizedLogin(login) return this.converterPage.isStudent(sanitizedLogin) } @Post('add-subscription-to-student') async addSubscriptionToStudent(@Body() { login }: AddSubscriptionToStudent): Promise { const sanitizedLogin = this.getSanitizedLogin(login) return this.converterPage.addSubscriptionToStudent(sanitizedLogin) } @Post('add-external-subscription-to-patient') async addExternalSubscriptionToPatient( @Body() { email, document_number, payment_id, name, cell_phone, country_code }: AddExternalSubscriptionToPatient, ): Promise { const login = this.getSanitizedLogin(document_number || email) const documentNumberSanitized = keepNumbers(document_number || '') const patient = plainToClass(Patient, { document_number: documentNumberSanitized, cell_phone: keepNumbers(cell_phone), email: email.trim(), name: name.trim(), login, country_code, }) return this.converterPage.addExternalSubscriptionToPatient(patient, payment_id) } private getSanitizedLogin(login: string): string { const sanitized = !isEmail(login) ? keepNumbers(login) : login.trim() if (isBlank(sanitized)) throw new RequiredArgumentError('login') return sanitized } }