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

#1837 - Fix missing details on submitting Designation Request #1848

Merged
merged 6 commits into from
Mar 30, 2023
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
Expand Up @@ -16,7 +16,11 @@ import {
IsInstitutionAdmin,
UserToken,
} from "../../auth/decorators";
import { DesignationAgreementService, FormService } from "../../services";
import {
DesignationAgreementService,
FormService,
InstitutionService,
} from "../../services";
import {
DesignationAgreementAPIOutDTO,
DesignationAgreementDetailsAPIOutDTO,
Expand Down Expand Up @@ -45,6 +49,7 @@ export class DesignationAgreementInstitutionsController extends BaseController {
constructor(
private readonly designationAgreementService: DesignationAgreementService,
private readonly formService: FormService,
private readonly institutionService: InstitutionService,
private readonly designationAgreementControllerService: DesignationAgreementControllerService,
) {
super();
Expand Down Expand Up @@ -81,6 +86,12 @@ export class DesignationAgreementInstitutionsController extends BaseController {
"User does not have the rights to create a designation agreement.",
);
}

// Check if institution is private and append it to the payload.
payload.isBCPrivate = await this.institutionService.isPrivateInstitution(
userToken.authorizations.institutionId,
);

// Validate the dynamic data submission.
const submissionResult = await this.formService.dryRunSubmission(
FormNames.DesignationAgreementDetails,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ export class SubmitDesignationAgreementAPIInDTO {
@ValidateNested({ each: true })
@Type(() => SubmittedLocationsAPIInDTO)
locations: SubmittedLocationsAPIInDTO[];
/**
* isBCPrivate is part of the form and defines if the dynamic area of the form.io definition will be visible or not, which also will impact the validation using the dryrun.
* Since this value has the source of truth on the institution, it must be populated by the API prior to the dryrun validation and it is part of DTO to make explicit its place in the form payload submitted but it will also be ignored by Nestjs because it does not have a decorator.
*/
isBCPrivate: boolean;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
INSTITUTION_USER_ALREADY_EXISTS,
LEGAL_SIGNING_AUTHORITY_EXIST,
} from "../../constants";
import { INSTITUTION_TYPE_BC_PRIVATE } from "@sims/sims-db/constant";
import { InstitutionUserAuthService } from "../institution-user-auth/institution-user-auth.service";
import { UserService } from "../user/user.service";

Expand Down Expand Up @@ -694,6 +695,37 @@ export class InstitutionService extends RecordDataModelService<Institution> {
.getOne();
}

/**
* Check if institution is private.
* @param institutionId is the institution id.
* @returns boolean true if institution is private, else false.
*/
async isPrivateInstitution(institutionId: number): Promise<boolean> {
const institutionType = await this.getInstitutionTypeById(institutionId);
return INSTITUTION_TYPE_BC_PRIVATE === institutionType.institutionType.id;
}

/**
* Get the institutionType by institution id.
* @param institutionId Institution id.
* @returns Institution retrieved, if found, otherwise returns null.
*/
async getInstitutionTypeById(institutionId: number): Promise<Institution> {
return this.repo.findOne({
Copy link
Collaborator

Choose a reason for hiding this comment

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

if the where is just an id, we can use findonebyId instead of findOne, just a suggestion

Copy link
Collaborator

Choose a reason for hiding this comment

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

findOneById is marked as deprecated. The one to be used instead would be findOneBy but it does not seem to have the select options only the where

findOneBy(where: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[]): Promise<Entity | null>

Copy link
Collaborator

Choose a reason for hiding this comment

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

yes just check its deprecated, yes we can use findOneBy but in this scenario, its kind of selects everything. I am okay to have it as it is.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In that case, I will leave it as it is.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The findOneBy here wouldn't bring the institutionType. AFAIK findOneBy doesn't allow you to specify the relations.

select: {
institutionType: {
id: true,
},
},
relations: {
institutionType: true,
},
where: {
id: institutionId,
},
});
}

/**
* Get the basic info of the institution by ID.
* @param institutionId Institution id.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { MigrationInterface, QueryRunner } from "typeorm";
import { getSQLFileData } from "../utilities/sqlLoader";

export class DesignationAgreementsTableAlterSubmittedData1680120153698
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
getSQLFileData(
"Update-col-submitted-data-drop-not-null.sql",
"DesignationAgreements",
),
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
getSQLFileData(
"Update-col-submitted-data-add-not-null.sql",
"DesignationAgreements",
),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Updated the submitted_data column from NULL to NOT NULL
ALTER TABLE
sims.designation_agreements
ALTER COLUMN
submitted_data
SET
NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Updated the submitted_data column from NOT NULL to NULL
ALTER TABLE
sims.designation_agreements
ALTER COLUMN
submitted_data DROP NOT NULL;
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ export class DesignationAgreement extends RecordDataModel {
@Column({
name: "submitted_data",
type: "jsonb",
nullable: false,
nullable: true,
})
submittedData: any;
submittedData: unknown;
/**
* Current status of the designation agreement.
*/
Expand Down