From 8b943e9fa924e1ccd08359554e6e282314914c0a Mon Sep 17 00:00:00 2001 From: Kar Rui Lau Date: Thu, 15 Jul 2021 15:23:23 +0800 Subject: [PATCH 1/4] ref: move billing related types to root shared folder --- shared/types/billing.ts | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 shared/types/billing.ts diff --git a/shared/types/billing.ts b/shared/types/billing.ts new file mode 100644 index 0000000000..55f227ecc0 --- /dev/null +++ b/shared/types/billing.ts @@ -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 +} + +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 + yr: string + mth: string +} + +export type BillingInfoDto = { loginStats: FormBillingStatistic[] } From 17bc758f176b5bd088c31351cf8e3452e0fde819 Mon Sep 17 00:00:00 2001 From: Kar Rui Lau Date: Thu, 15 Jul 2021 15:24:03 +0800 Subject: [PATCH 2/4] ref(server): extend from shared types --- .../__tests__/login.server.model.spec.ts | 6 ++++-- src/types/api/billing.ts | 13 +----------- src/types/login.ts | 21 +++++++------------ 3 files changed, 13 insertions(+), 27 deletions(-) diff --git a/src/app/models/__tests__/login.server.model.spec.ts b/src/app/models/__tests__/login.server.model.spec.ts index 1c625b1d2f..630ac41245 100644 --- a/src/app/models/__tests__/login.server.model.spec.ts +++ b/src/app/models/__tests__/login.server.model.spec.ts @@ -9,6 +9,7 @@ import { AuthType, IFormSchema, ILogin, + ILoginSchema, IPopulatedForm, IUserSchema, } from 'src/types' @@ -163,6 +164,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') }) @@ -175,7 +177,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 @@ -219,7 +221,7 @@ describe('login.server.model', () => { esrvcId: VALID_ESRVC_ID, created: FUTURE_DATE, }, - ] + ] as ILoginSchema[] await LoginModel.insertMany(mockLoginDocuments) testUser = user diff --git a/src/types/api/billing.ts b/src/types/api/billing.ts index 60e3f03fcb..d25a7da1c5 100644 --- a/src/types/api/billing.ts +++ b/src/types/api/billing.ts @@ -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' diff --git a/src/types/login.ts b/src/types/login.ts index a9764557b6..fdb3786cb4 100644 --- a/src/types/login.ts +++ b/src/types/login.ts @@ -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 { +export 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 { aggregateLoginStats: ( esrvcId: string, From 44cd711f9e8e2edec4de632a099b8deaac89fb82 Mon Sep 17 00:00:00 2001 From: Kar Rui Lau Date: Thu, 15 Jul 2021 15:24:41 +0800 Subject: [PATCH 3/4] ref(client): use shared types --- src/public/services/BillingService.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/public/services/BillingService.ts b/src/public/services/BillingService.ts index 34093d7ad1..1222b734f8 100644 --- a/src/public/services/BillingService.ts +++ b/src/public/services/BillingService.ts @@ -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' From 45937666de76b0b643a29db46aa768e7b038d454 Mon Sep 17 00:00:00 2001 From: Kar Rui Lau Date: Tue, 27 Jul 2021 10:47:49 +0800 Subject: [PATCH 4/4] feat: remove export for ILogin type --- src/app/models/__tests__/login.server.model.spec.ts | 3 +-- src/types/login.ts | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/app/models/__tests__/login.server.model.spec.ts b/src/app/models/__tests__/login.server.model.spec.ts index 630ac41245..fc27914a03 100644 --- a/src/app/models/__tests__/login.server.model.spec.ts +++ b/src/app/models/__tests__/login.server.model.spec.ts @@ -8,7 +8,6 @@ import getLoginModel from 'src/app/models/login.server.model' import { AuthType, IFormSchema, - ILogin, ILoginSchema, IPopulatedForm, IUserSchema, @@ -24,7 +23,7 @@ describe('login.server.model', () => { afterAll(async () => await dbHandler.closeDatabase()) describe('Schema', () => { - const DEFAULT_PARAMS: ILogin = { + const DEFAULT_PARAMS: mongoose.LeanDocument = { admin: new ObjectId(), agency: new ObjectId(), authType: AuthType.SP, diff --git a/src/types/login.ts b/src/types/login.ts index fdb3786cb4..f41690f811 100644 --- a/src/types/login.ts +++ b/src/types/login.ts @@ -6,7 +6,7 @@ import { IAgencySchema } from './agency' import { IFormSchema, IPopulatedForm } from './form' import { IUserSchema } from './user' -export interface ILogin extends LoginBase { +interface ILogin extends LoginBase { admin: IUserSchema['_id'] form: IFormSchema['_id'] agency: IAgencySchema['_id']