diff --git a/apps/air-discount-scheme/backend/src/app/modules/discount/discount.service.ts b/apps/air-discount-scheme/backend/src/app/modules/discount/discount.service.ts index 993a82013473..9ccc135e8265 100644 --- a/apps/air-discount-scheme/backend/src/app/modules/discount/discount.service.ts +++ b/apps/air-discount-scheme/backend/src/app/modules/discount/discount.service.ts @@ -17,6 +17,8 @@ import { UserService } from '../user/user.service' import type { User as AuthUser } from '@island.is/auth-nest-tools' import { ExplicitFlight } from './dto/ExplicitFlight.dto' import { CreateSuperExplicitDiscountCodeParams } from './dto' +import type { Logger } from '@island.is/logging' +import { LOGGER_PROVIDER } from '@island.is/logging' interface CachedDiscount { user: User @@ -47,6 +49,8 @@ export class DiscountService { @InjectModel(ExplicitCode) private explicitModel: typeof ExplicitCode, + @Inject(LOGGER_PROVIDER) + private readonly logger: Logger, private readonly userService: UserService, ) {} @@ -197,13 +201,19 @@ export class DiscountService { unConnectedFlights: Flight[] | ExplicitFlight[], isExplicit: boolean, flightLegs = 1, + isManual?: boolean, ): Promise | null> { const user = await this.userService.getUserInfoByNationalId( nationalId, auth, + isExplicit, + isManual, ) if (!user) { + this.logger.warn('User by national id not found for explicit discount.', { + category: 'ads-backend', + }) return null } // overwrite credit since validation may return 0 depending on what the problem is @@ -212,10 +222,19 @@ export class DiscountService { user.fund.credit = 2 //making sure we can get flight from and to user.fund.total = 2 } else { + this.logger.warn( + `User fund used requirements not met: ${user.fund.used}.`, + { + category: 'ads-backend', + }, + ) return null } } if (user.fund.credit === 0 && user.fund.total !== undefined) { + this.logger.warn(`User fund no credit, has total: ${user.fund.total}.`, { + category: 'ads-backend', + }) return null } @@ -477,6 +496,7 @@ export class DiscountService { ], } + const isManual = true const discount = await this.createExplicitDiscountCode( auth, body.nationalId, @@ -487,6 +507,7 @@ export class DiscountService { body.needsConnectionFlight ? [flight] : [], isExplicit, 1, + isManual, ) if (!discount) { throw new Error(`Could not create explicit discount`) diff --git a/apps/air-discount-scheme/backend/src/app/modules/discount/test/unit/discount.service.spec.ts b/apps/air-discount-scheme/backend/src/app/modules/discount/test/unit/discount.service.spec.ts index a5b95b9ca4b4..46c9da2b5f8f 100644 --- a/apps/air-discount-scheme/backend/src/app/modules/discount/test/unit/discount.service.spec.ts +++ b/apps/air-discount-scheme/backend/src/app/modules/discount/test/unit/discount.service.spec.ts @@ -64,7 +64,9 @@ describe('DiscountService', () => { { provide: LOGGER_PROVIDER, useClass: jest.fn(() => ({ - error: () => ({}), + error: jest.fn(), + info: jest.fn(), + warn: jest.fn(), })), }, { diff --git a/apps/air-discount-scheme/backend/src/app/modules/user/user.service.ts b/apps/air-discount-scheme/backend/src/app/modules/user/user.service.ts index 0761e5880450..034f80c06872 100644 --- a/apps/air-discount-scheme/backend/src/app/modules/user/user.service.ts +++ b/apps/air-discount-scheme/backend/src/app/modules/user/user.service.ts @@ -17,6 +17,12 @@ const ONE_WEEK = 604800 // seconds const CACHE_KEY = 'userService' const MAX_AGE_LIMIT = 18 +const DEFAULT_FUND: Fund = { + credit: 2, + total: 2, + used: 0, +} + interface CustodianCache { custodians: Array } @@ -42,6 +48,7 @@ export class UserService { private async getFund( user: NationalRegistryUser, auth?: AuthUser, + isManual?: boolean, ): Promise { const { used, unused, total } = await this.flightService.countThisYearsFlightLegsByNationalId( @@ -49,7 +56,7 @@ export class UserService { ) let meetsADSRequirements = false - if (this.flightService.isADSPostalCode(user.postalcode)) { + if (this.flightService.isADSPostalCode(user.postalcode) || isManual) { meetsADSRequirements = true } else if (info(user.nationalId).age < MAX_AGE_LIMIT) { // NationalId is a minor and doesn't live in ADS postal codes. @@ -95,20 +102,33 @@ export class UserService { nationalId: string, model: new (user: NationalRegistryUser, fund: Fund) => T, auth: AuthUser, + isExplicit?: boolean, + isManual?: boolean, ): Promise { const user = await this.nationalRegistryService.getUser(nationalId, auth) if (!user) { return null } - const fund = await this.getFund(user, auth) + if (isExplicit) { + return new model(user, DEFAULT_FUND) + } + const fund = await this.getFund(user, auth, isManual) return new model(user, fund) } async getUserInfoByNationalId( nationalId: string, auth: AuthUser, + isExplicit?: boolean, + isManual?: boolean, ): Promise { - return this.getUserByNationalId(nationalId, User, auth) + return this.getUserByNationalId( + nationalId, + User, + auth, + isExplicit, + isManual, + ) } async getMultipleUsersByNationalIdArray(