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: ORV2-2353 / ORV2-2351 Credit Account Hold and Close #1451

Merged
merged 3 commits into from
Jun 28, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
ValidatorConstraint,
ValidatorConstraintInterface,
ValidationArguments,
} from 'class-validator';
import {
CreditAccountStatus,
CreditAccountStatusValidType,
} from '../enum/credit-account-status-type.enum';

@ValidatorConstraint({
name: 'CreditAccountStatusCommentConstraint',
async: false,
})
export class CreditAccountStatusCommentConstraint
implements ValidatorConstraintInterface
{
validate(comment: string | undefined, args: ValidationArguments) {
const creditAccountStatusType = (
args.object as {
creditAccountStatusType?: CreditAccountStatusValidType;
}
).creditAccountStatusType; // Access the searchString property from the same object

// If CreditAccountStatusType.ACCOUNT_CLOSED or CreditAccountStatusType.ACCOUNT_ON_HOLD , comment should exists
if (
(creditAccountStatusType === CreditAccountStatus.ACCOUNT_CLOSED ||
creditAccountStatusType === CreditAccountStatus.ACCOUNT_ON_HOLD) &&
!comment
) {
return false;
}

return true;
}

defaultMessage() {
return `Comment is required when activity type is ${CreditAccountStatus.ACCOUNT_CLOSED} or ${CreditAccountStatus.ACCOUNT_ON_HOLD}`;
}
}
25 changes: 19 additions & 6 deletions vehicles/src/common/enum/credit-account-status-type.enum.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
export enum CreditAccountStatusType {
ACCOUNT_ON_HOLD = 'ONHOLD',
ACCOUNT_ACTIVE = 'ACTIVE',
ACCOUNT_SETUP_FAIL = 'SETUP_FAIL',
ACCOUNT_CLOSED = 'CLOSED',
}
export const CreditAccountStatus = {
ACCOUNT_ON_HOLD: 'ONHOLD',
ACCOUNT_ACTIVE: 'ACTIVE',
ACCOUNT_CLOSED: 'CLOSED',
ACCOUNT_SETUP_FAIL: 'SETUP_FAIL',
} as const;

export type CreditAccountStatusType =
(typeof CreditAccountStatus)[keyof typeof CreditAccountStatus];

// Manually constructing the CreditAccountStatusValidType as a const
export const CreditAccountStatusValid = {
ACCOUNT_ON_HOLD: CreditAccountStatus.ACCOUNT_ON_HOLD,
ACCOUNT_ACTIVE: CreditAccountStatus.ACCOUNT_ACTIVE,
ACCOUNT_CLOSED: CreditAccountStatus.ACCOUNT_CLOSED,
} as const;

export type CreditAccountStatusValidType =
(typeof CreditAccountStatus)[keyof typeof CreditAccountStatusValid];
37 changes: 34 additions & 3 deletions vehicles/src/common/helper/credit-account.helper.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,47 @@
import { CreditAccount } from '../../modules/credit-account/entities/credit-account.entity';
import { CreditAccountStatusType } from '../enum/credit-account-status-type.enum';
import { CreditAccountActivityType } from '../enum/credit-account-activity-type.enum';
import {
CreditAccountStatus,
CreditAccountStatusType,
CreditAccountStatusValid,
} from '../enum/credit-account-status-type.enum';

export const isActiveCreditAccount = (creditAccount: CreditAccount) => {
return (
creditAccount?.creditAccountStatusType ===
CreditAccountStatusType.ACCOUNT_ACTIVE
CreditAccountStatus.ACCOUNT_ACTIVE
);
};

export const isClosedCreditAccount = (creditAccount: CreditAccount) => {
return (
creditAccount?.creditAccountStatusType ===
CreditAccountStatusType.ACCOUNT_CLOSED
CreditAccountStatus.ACCOUNT_CLOSED
);
};

export const isOnHoldCreditAccount = (creditAccount: CreditAccount) => {
return (
creditAccount?.creditAccountStatusType ===
CreditAccountStatus.ACCOUNT_ON_HOLD
);
};

export const getCreditAccountActivityType = (
creditAccount: CreditAccount,
statusToUpdateTo: CreditAccountStatusType,
) => {
switch (statusToUpdateTo) {
case CreditAccountStatusValid.ACCOUNT_ACTIVE:
if (isClosedCreditAccount(creditAccount)) {
return CreditAccountActivityType.ACCOUNT_REOPENED;
} else if (isOnHoldCreditAccount(creditAccount)) {
return CreditAccountActivityType.ACCOUNT_HOLD_REMOVED;
}
break;
case CreditAccountStatusValid.ACCOUNT_ON_HOLD:
return CreditAccountActivityType.ACCOUNT_ON_HOLD;
case CreditAccountStatusValid.ACCOUNT_CLOSED:
return CreditAccountActivityType.ACCOUNT_CLOSED;
}
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Body, Controller, Param, Post, Req } from '@nestjs/common';
import { Body, Controller, Get, Param, Post, Put, Req } from '@nestjs/common';
import {
ApiBadRequestResponse,
ApiBearerAuth,
Expand All @@ -21,6 +21,8 @@ import { CreditAccountService } from './credit-account.service';
import { CreateCreditAccountDto } from './dto/request/create-credit-account.dto';
import { ReadCreditAccountUserDto } from './dto/response/read-credit-account-user.dto';
import { ReadCreditAccountDto } from './dto/response/read-credit-account.dto';
import { CreditAccountIdPathParamDto } from './dto/request/pathParam/creditAccountUsers.path-params.dto';
import { UpdateCreditAccountStatusDto } from './dto/request/update-credit-account-status.dto';

@ApiBearerAuth()
@ApiTags('Credit Accounts')
Expand Down Expand Up @@ -84,7 +86,7 @@ export class CreditAccountController {
description: 'The retrieved credit account.',
type: ReadCreditAccountDto,
})
// @Get() - Uncomment when feature is ready
@Get()
@Roles(Role.READ_CREDIT_ACCOUNT)
async getCreditAccount(
@Req() request: Request,
Expand All @@ -100,4 +102,40 @@ export class CreditAccountController {
}
return readCreditAccountDto;
}

/**
* Updates the status of a credit account user.
*
* @param { companyId } - The companyId path parameter.
* @param { creditAccountId } - The creditAccountId path parameter.
* @param { creditAccountStatusType, comment } - The DTO containing status type and comment to update a credit account user.
* @returns The result of the update operation.
*/
@ApiOperation({
summary: 'Updates the status of a credit account user.',
description:
'Updates the status of a credit account user, enforcing authentication.',
})
@ApiOkResponse({
description: 'The updated credit account status details.',
type: ReadCreditAccountUserDto,
})
@Put(':creditAccountId/status')
@Roles(Role.WRITE_CREDIT_ACCOUNT)
async updateCreditAccountStatus(
@Req() request: Request,
@Param() { companyId, creditAccountId }: CreditAccountIdPathParamDto,
@Body() { creditAccountStatusType, comment }: UpdateCreditAccountStatusDto,
): Promise<ReadCreditAccountDto> {
const currentUser = request.user as IUserJWT;
return await this.creditAccountService.updateCreditAccountStatus(
currentUser,
{
creditAccountHolderId: companyId,
creditAccountId,
statusToUpdateTo: creditAccountStatusType,
comment: comment,
},
);
}
}
Loading