Skip to content

Commit

Permalink
refactor(billing): updated billing fe/be to use dto
Browse files Browse the repository at this point in the history
  • Loading branch information
seaerchin committed Apr 1, 2021
1 parent 796447c commit 1f08ca9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
10 changes: 3 additions & 7 deletions src/app/modules/billing/billing.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import { StatusCodes } from 'http-status-codes'
import moment from 'moment-timezone'

import { createLoggerWithLabel } from '../../../config/logger'
import {
BillingInformationDto,
BillingQueryDto,
ErrorDto,
} from '../../../types/api'
import { BillingInfoDto, BillingQueryDto, ErrorDto } from '../../../types/api'
import { createReqMeta } from '../../utils/request'

import { BillingFactory } from './billing.factory'
Expand All @@ -25,7 +21,7 @@ const logger = createLoggerWithLabel(module)
*/
export const handleGetBillInfo: RequestHandler<
ParamsDictionary,
ErrorDto | BillingInformationDto,
ErrorDto | BillingInfoDto,
unknown,
BillingQueryDto
> = async (req, res) => {
Expand Down Expand Up @@ -55,7 +51,7 @@ export const handleGetBillInfo: RequestHandler<
})
return res
.status(StatusCodes.INTERNAL_SERVER_ERROR)
.json('Error in retrieving billing records')
.json({ message: 'Error in retrieving billing records' })
}

// Retrieved login stats successfully.
Expand Down
20 changes: 6 additions & 14 deletions src/public/services/BillingService.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
import axios from 'axios'

import { BillingInformationDto } from '../../types/api/billing'
import { BillingInfoDto, BillingQueryDto } from '../../types/api/billing'

// Exported for testing
export const BILLING_ENDPOINT = '/billing'

/**
* Gets the billing information for the given month and year
* @param yr The year to get the billing information for
* @param mth The month to get the billing information for
* @param esrvcId The id of the form
* @param billingQueryParams The formId and the specific month to get the information for
* @returns Promise<BillingResult> The billing statistics of the given month
*/
export const getBillingInfo = (
yr: string,
mth: string,
esrvcId: string,
): Promise<BillingInformationDto> => {
billingQueryParams: BillingQueryDto,
): Promise<BillingInfoDto> => {
return axios
.get<BillingInformationDto>(BILLING_ENDPOINT, {
params: {
yr,
mth,
esrvcId,
},
.get<BillingInfoDto>(BILLING_ENDPOINT, {
params: billingQueryParams,
})
.then(({ data }) => data)
}
17 changes: 15 additions & 2 deletions src/types/api/billing.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import { LoginStatistic } from '../../types'
import { AuthType } from '../../types'

// yr: The year to get the billing information for
// mth: The month to get the billing information for
// esrvcId: The id of the form
export type BillingQueryDto = {
esrvcId: string
yr: string
mth: string
}

export type BillingInformationDto = { loginStats: LoginStatistic[] }
// NOTE: LoginStatistic is defined here and in src/types/login.
// This is to allow the communication layer (the dto) to be agnostic of intermediate representations.
type LoginStatistic = {
adminEmail: string
formName: string
total: number
formId: string
authType: AuthType
}

export type BillingInfoDto = { loginStats: LoginStatistic[] }
1 change: 1 addition & 0 deletions src/types/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './core'
export * from './form'
export * from './billing'
export * from './examples'

0 comments on commit 1f08ca9

Please sign in to comment.