Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(shared-types): move billing related types to shared folder #2400

Merged
merged 4 commits into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -163,6 +163,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 @@ -175,7 +176,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 @@ -219,7 +220,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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By moving created to ILoginSchema, is it still optional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, since this typing is used for document creation, and we don't want to enforce a created prop to be assigned for document creation.

We should actually create a LoginDocument typing for the object that is retrieved from the database (with all the defaults populated, and have the optional types converted to required), but there are currently no cases where that is needed and thus not done yet.

}

export type LoginStatistic = FormBillingStatistic

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