Skip to content

Commit

Permalink
ORV2-2542 Credit Accounts - Roles and Permissions - Part 2 (#1520)
Browse files Browse the repository at this point in the history
  • Loading branch information
praju-aot authored Jul 26, 2024
1 parent 89f3762 commit 79f5a52
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { IUserJWT } from '../../common/interface/user-jwt.interface';
import { CompanyIdPathParamDto } from '../common/dto/request/pathParam/companyId.path-param.dto';
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';
Expand Down Expand Up @@ -103,7 +102,7 @@ export class CreditAccountController {
description: 'The retrieved credit account.',
type: ReadCreditAccountMetadataDto,
})
@Get('meta-data')
@Get()
@Roles(Role.READ_CREDIT_ACCOUNT)
async getCreditAccountMetadata(
@Req() request: Request,
Expand Down
75 changes: 75 additions & 0 deletions vehicles/src/modules/credit-account/credit-account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,19 @@ export class CreditAccountService {
) {
// Throw exception if companyId is a Credit Account User and user is Company Admin.
throw new ForbiddenException();
} else if (
creditAccount?.company?.companyId === companyId &&
creditAccount?.creditAccountStatusType ===
CreditAccountStatus.ACCOUNT_CLOSED &&
!doesUserHaveAuthGroup(currentUser.orbcUserAuthGroup, [
IDIRUserAuthGroup.HQ_ADMINISTRATOR,
IDIRUserAuthGroup.SYSTEM_ADMINISTRATOR,
IDIRUserAuthGroup.FINANCE,
IDIRUserAuthGroup.PPC_CLERK,
IDIRUserAuthGroup.PPC_SUPERVISOR,
])
) {
throw new DataNotFoundException();
}

const readCreditAccountDto = await this.classMapper.mapAsync(
Expand Down Expand Up @@ -319,6 +332,19 @@ export class CreditAccountService {

if (!creditAccount) {
throw new DataNotFoundException();
} else if (
creditAccount?.company?.companyId === companyId &&
creditAccount?.creditAccountStatusType ===
CreditAccountStatus.ACCOUNT_CLOSED &&
!doesUserHaveAuthGroup(currentUser.orbcUserAuthGroup, [
IDIRUserAuthGroup.HQ_ADMINISTRATOR,
IDIRUserAuthGroup.SYSTEM_ADMINISTRATOR,
IDIRUserAuthGroup.FINANCE,
IDIRUserAuthGroup.PPC_CLERK,
IDIRUserAuthGroup.PPC_SUPERVISOR,
])
) {
throw new DataNotFoundException();
}

creditAccount.creditAccountUsers =
Expand All @@ -337,6 +363,15 @@ export class CreditAccountService {
readCreditAccountMetadataDto.userType =
CreditAccountUserType.ACCOUNT_USER;
}
if (
creditAccount?.creditAccountStatusType ===
CreditAccountStatus.ACCOUNT_ACTIVE &&
!creditAccount?.company.isSuspended
) {
readCreditAccountMetadataDto.isValidPaymentMethod = true;
} else {
readCreditAccountMetadataDto.isValidPaymentMethod = false;
}
return readCreditAccountMetadataDto;
}

Expand Down Expand Up @@ -984,6 +1019,19 @@ export class CreditAccountService {
) {
// Throw exception if companyId is a Credit Account User and user is Company Admin.
throw new ForbiddenException();
} else if (
creditAccount?.company?.companyId === companyId &&
creditAccount?.creditAccountStatusType ===
CreditAccountStatus.ACCOUNT_CLOSED &&
!doesUserHaveAuthGroup(currentUser.orbcUserAuthGroup, [
IDIRUserAuthGroup.HQ_ADMINISTRATOR,
IDIRUserAuthGroup.SYSTEM_ADMINISTRATOR,
IDIRUserAuthGroup.FINANCE,
IDIRUserAuthGroup.PPC_CLERK,
IDIRUserAuthGroup.PPC_SUPERVISOR,
])
) {
throw new DataNotFoundException();
}

creditAccount.creditAccountUsers =
Expand Down Expand Up @@ -1044,6 +1092,29 @@ export class CreditAccountService {

if (!creditAccount) {
throw new DataNotFoundException();
} else if (
creditAccount?.creditAccountStatusType ===
CreditAccountStatus.ACCOUNT_ON_HOLD &&
doesUserHaveAuthGroup(currentUser.orbcUserAuthGroup, [
ClientUserAuthGroup.COMPANY_ADMINISTRATOR,
]) &&
creditAccount?.company.companyId === companyId
) {
// Throw exception if companyId is a Credit Account User and user is Company Admin.
throw new ForbiddenException();
} else if (
creditAccount?.company?.companyId === companyId &&
creditAccount?.creditAccountStatusType ===
CreditAccountStatus.ACCOUNT_CLOSED &&
!doesUserHaveAuthGroup(currentUser.orbcUserAuthGroup, [
IDIRUserAuthGroup.HQ_ADMINISTRATOR,
IDIRUserAuthGroup.SYSTEM_ADMINISTRATOR,
IDIRUserAuthGroup.FINANCE,
IDIRUserAuthGroup.PPC_CLERK,
IDIRUserAuthGroup.PPC_SUPERVISOR,
])
) {
throw new DataNotFoundException();
} else if (
doesUserHaveAuthGroup(currentUser.orbcUserAuthGroup, [
ClientUserAuthGroup.COMPANY_ADMINISTRATOR,
Expand Down Expand Up @@ -1209,6 +1280,10 @@ export class CreditAccountService {
case IDIRUserAuthGroup.HQ_ADMINISTRATOR:
case IDIRUserAuthGroup.PPC_CLERK:
case IDIRUserAuthGroup.PPC_SUPERVISOR:
return {
company: true,
creditAccountUsers: { company: true },
};
case ClientUserAuthGroup.COMPANY_ADMINISTRATOR:
case ClientUserAuthGroup.PERMIT_APPLICANT:
return { company: true };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@ export class ReadCreditAccountMetadataDto {
example: CreditAccountUserType.ACCOUNT_HOLDER,
})
userType: CreditAccountUserType;

@AutoMap()
@ApiProperty({
description:
'Indicates whether the credit account can be used as a valid payment method.',
example: false,
})
isValidPaymentMethod: boolean;
}

0 comments on commit 79f5a52

Please sign in to comment.