Skip to content

Commit

Permalink
feat(shared-types): move billing related types to shared folder (#2400)
Browse files Browse the repository at this point in the history
* ref: move billing related types to root shared folder

* ref(server): extend from shared types

* ref(client): use shared types

* feat: remove export for ILogin type
  • Loading branch information
karrui authored Aug 2, 2021
1 parent 5935cb6 commit 2726ee6
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 30 deletions.
37 changes: 37 additions & 0 deletions shared/types/billing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { AgencyDto } from './agency'
import { FormDto } from './form/form'
import { UserDto } from './user'

/**
* The name `Login` may cause confusion.
* This type relates to the data stored when a form respondent logs in to the
* form via any of the public form auth methods (Singpass, Corppass, MyInfo,
* etc).
*/
export type LoginBase = {
admin: UserDto['_id']
form: FormDto['_id']
agency: AgencyDto['_id']
authType: FormDto['authType']
// A login must be for a form that has an esrvcId.
esrvcId: NonNullable<FormDto['esrvcId']>
}

export type FormBillingStatistic = {
adminEmail: UserDto['email']
formName: FormDto['title']
formId: FormDto['_id']
authType: FormDto['authType']
total: number
}

// 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: NonNullable<FormDto['esrvcId']>
yr: string
mth: string
}

export type BillingInfoDto = { loginStats: FormBillingStatistic[] }
9 changes: 5 additions & 4 deletions src/app/models/__tests__/login.server.model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import getLoginModel from 'src/app/models/login.server.model'
import {
AuthType,
IFormSchema,
ILogin,
ILoginSchema,
IPopulatedForm,
IUserSchema,
} from 'src/types'
Expand All @@ -23,7 +23,7 @@ describe('login.server.model', () => {
afterAll(async () => await dbHandler.closeDatabase())

describe('Schema', () => {
const DEFAULT_PARAMS: ILogin = {
const DEFAULT_PARAMS: mongoose.LeanDocument<ILoginSchema> = {
admin: new ObjectId(),
agency: new ObjectId(),
authType: AuthType.SP,
Expand Down Expand Up @@ -150,6 +150,7 @@ describe('login.server.model', () => {

it('should reject when the form does not contain an authType', async () => {
await expect(
// @ts-ignore
LoginModel.addLoginFromForm(omit(fullForm, 'authType')),
).rejects.toThrow('Form does not contain authType or e-service ID')
})
Expand All @@ -162,7 +163,7 @@ describe('login.server.model', () => {
const FUTURE_MOMENT = CURR_MOMENT.clone().add(2, 'years')
const FUTURE_DATE = FUTURE_MOMENT.toDate()

let mockLoginDocuments: ILogin[]
let mockLoginDocuments: ILoginSchema[]
let testUser: IUserSchema
let testForm: IFormSchema

Expand Down Expand Up @@ -206,7 +207,7 @@ describe('login.server.model', () => {
esrvcId: VALID_ESRVC_ID,
created: FUTURE_DATE,
},
]
] as ILoginSchema[]

await LoginModel.insertMany(mockLoginDocuments)
testUser = user
Expand Down
2 changes: 1 addition & 1 deletion src/public/services/BillingService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios'

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

// Exported for testing
export const BILLING_ENDPOINT = '/api/v3/billings'
Expand Down
13 changes: 1 addition & 12 deletions src/types/api/billing.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1 @@
import { LoginStatistic } 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 BillingInfoDto = { loginStats: LoginStatistic[] }
export { BillingQueryDto, BillingInfoDto } from '../../../shared/types/billing'
21 changes: 8 additions & 13 deletions src/types/login.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
import { Document, Model } from 'mongoose'

import { FormBillingStatistic, LoginBase } from '../../shared/types/billing'

import { IAgencySchema } from './agency'
import { AuthType, IFormSchema, IPopulatedForm } from './form'
import { IFormSchema, IPopulatedForm } from './form'
import { IUserSchema } from './user'

export interface ILogin {
interface ILogin extends LoginBase {
admin: IUserSchema['_id']
form: IFormSchema['_id']
agency: IAgencySchema['_id']
authType: AuthType
esrvcId: string
created?: Date
}

export interface ILoginSchema extends ILogin, Document {}

export type LoginStatistic = {
adminEmail: IUserSchema['email']
formName: IFormSchema['title']
total: number
formId: IFormSchema['_id']
authType: ILoginSchema['authType']
export interface ILoginSchema extends ILogin, Document {
created?: Date
}

export type LoginStatistic = FormBillingStatistic

export interface ILoginModel extends Model<ILoginSchema> {
aggregateLoginStats: (
esrvcId: string,
Expand Down

0 comments on commit 2726ee6

Please sign in to comment.