Skip to content

Commit

Permalink
Merge pull request #147 from bcgov/report-routes
Browse files Browse the repository at this point in the history
Report routes
  • Loading branch information
mgtennant authored Mar 29, 2024
2 parents 0afbe33 + b099c54 commit c3621bf
Show file tree
Hide file tree
Showing 34 changed files with 1,672 additions and 969 deletions.
22 changes: 5 additions & 17 deletions backend/src/document_data/document_data.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,6 @@ import { DocumentDataService } from './document_data.service';
export class DocumentDataController {
constructor(private readonly documentDataService: DocumentDataService) {}

@Post()
async create(
@Body()
data: {
body: CreateDocumentDataDto & {
provisionJsonArray: ProvisionJSON[];
variableJsonArray: VariableJSON[];
};
}
) {
const provArr = data.body.provisionJsonArray;
const varArr = data.body.variableJsonArray;
delete data.body['provisionJsonArray'];
delete data.body['variableJsonArray'];
return this.documentDataService.createOrUpdate(data.body, provArr, varArr);
}

@Get()
findAll() {
return this.documentDataService.findAll();
Expand Down Expand Up @@ -58,6 +41,11 @@ export class DocumentDataController {
return this.documentDataService.getProvisionsByDocTypeIdAndDtid(document_type_id, dtid);
}

@Get('get/:document_type_id/:dtid')
getDocData(@Param('document_type_id') document_type_id: number, @Param('dtid') dtid: number) {
return this.documentDataService.getProvisionsByDocTypeIdAndDtid(document_type_id, dtid);
}

@Get('get-enabled-provisions/:document_type_id/:dtid')
getEnabledProvisionsByDocTypeIdAndDtid(
@Param('document_type_id') document_type_id: number,
Expand Down
129 changes: 97 additions & 32 deletions backend/src/document_data/document_data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ProvisionService } from 'src/provision/provision.service';
import { DocumentTemplateService } from 'src/document_template/document_template.service';
import { DocumentType } from 'src/document_type/entities/document_type.entity';
import { DocumentTypeService } from 'src/document_type/document_type.service';
import { DocumentTypeProvision } from 'src/provision/entities/document_type_provision';

@Injectable()
export class DocumentDataService {
Expand All @@ -34,11 +35,12 @@ export class DocumentDataService {

async createOrUpdate(
documentDataDto: CreateDocumentDataDto,
provisionArray: { provision_id: number; free_text: string }[],
document_type_id: number,
provisionArray: { provision_id: number; doc_type_provision_id: number }[],
variableArray: { variable_id: number; variable_value: string }[]
): Promise<DocumentData> {
console.log(variableArray);
const { dtid, document_type_id } = documentDataDto;
const { dtid } = documentDataDto;
const documentData: DocumentData = await this.documentDataRepository.findOne({
where: { dtid: dtid, document_type: { id: document_type_id } },
relations: [
Expand Down Expand Up @@ -71,19 +73,23 @@ export class DocumentDataService {
);
}
// else create new documentData
const documentTemplate = await this.documentTemplateService.findActiveByDocumentType(
documentDataDto.document_type_id
);
const documentTemplate = await this.documentTemplateService.findActiveByDocumentType(document_type_id);
documentDataDto['template_id'] = documentTemplate ? documentTemplate.id : null;
documentDataDto['document_type'] = await this.documentTypeService.findById(document_type_id);
delete documentDataDto['document_type_id'];
const documentType = await this.documentTypeService.findById(document_type_id);
const newDocumentData: DocumentData = this.documentDataRepository.create(documentDataDto);
newDocumentData.document_type = documentType;
const updatedDocumentData = await this.documentDataRepository.save(newDocumentData);

const documentDataProvisions = provisionArray.map(({ provision_id, free_text }) => {
const documentTypeProvisions: DocumentTypeProvision[] =
await this.provisionService.getSimpleDocTypeProvisionsByDocTypeId(document_type_id);

const documentDataProvisions = provisionArray.map(({ provision_id }) => {
const documentProvision = documentProvisions.find((provision) => provision.id === provision_id);
const documentDataProvision = new DocumentDataProvision();
documentDataProvision.document_data = updatedDocumentData;
documentDataProvision.document_type_provision = documentTypeProvisions.find(
(dtp) => dtp.provision.id === provision_id
);
documentDataProvision.document_provision = documentProvision;
return documentDataProvision;
});
Expand All @@ -108,7 +114,7 @@ export class DocumentDataService {
async updateDocumentData(
documentDataDto: CreateDocumentDataDto,
documentData: DocumentData,
provisionArray: { provision_id: number; free_text: string }[],
provisionArray: { provision_id: number; doc_type_provision_id: number }[],
variableArray: { variable_id: number; variable_value: string }[],
documentProvisions: Provision[],
documentVariables: ProvisionVariable[]
Expand All @@ -126,17 +132,21 @@ export class DocumentDataService {
(p) => p.document_provision.id === provision.provision_id
);
const documentProvision = documentProvisions.find((p) => p.id === provision.provision_id);
const documentTypeProvision = await this.provisionService.getDocTypeProvisionById(
provision.doc_type_provision_id
);

if (documentDataProvision) {
// Update an existing DocumentDataProvision entry
await this.documentDataProvisionRepository.save(documentDataProvision);
// await this.documentDataProvisionRepository.save(documentDataProvision);
} else if (!documentDataProvision) {
// No data found for this specific provision so create a new entry in DocumentDataProvision
const documentProvisionToAdd = await this.documentProvisionRepository.findOneBy({
id: provision.provision_id,
});
const newDocumentDataProvision: DocumentDataProvision = this.documentDataProvisionRepository.create({
document_provision: documentProvisionToAdd,
document_type_provision: documentTypeProvision,
document_data: updatedDocumentData,
});
await this.documentDataProvisionRepository.save(newDocumentDataProvision);
Expand Down Expand Up @@ -173,7 +183,7 @@ export class DocumentDataService {

async deleteDataVarsAndProvs(
documentData: DocumentData,
provisionArray: { provision_id: number; free_text: string }[],
provisionArray: { provision_id: number; doc_type_provision_id: number }[],
variableArray: { variable_id: number; variable_value: string }[]
) {
// Delete data provisions that have been removed by the user
Expand Down Expand Up @@ -205,12 +215,8 @@ export class DocumentDataService {
}

// Used by the search page.
// Variant data is persisted so only return the active variant foreach dtid.
async findAll(): Promise<DocumentData[]> {
return await this.documentDataRepository.find({
where: {
active: true,
},
relations: ['document_type'],
});
}
Expand Down Expand Up @@ -287,6 +293,7 @@ export class DocumentDataService {
});
}

// delete
async getVariablesByDtidAndDocType(dtid: number, document_type_id: number) {
try {
const documentData: DocumentData = await this.documentDataRepository.findOne({
Expand All @@ -301,12 +308,12 @@ export class DocumentDataService {
});
// if the documentData doesn't exist yet, return null. This null value is caught elsewhere.
if (!documentData) {
return null;
return { variables: [], variableIds: [] };
}
// saved variables attached to the documentData entry
const existingDataVariables: DocumentDataVariable[] = documentData.document_data_variables;
// all variables associated with the variant
const variables: ProvisionVariable[] = await this.provisionService.getVariablesByDocumentTypeId(document_type_id);
const variables: ProvisionVariable[] = await this.provisionService.getVariablesByDocumentTypeId();
// inserting the existing variable_values to the set of all variables
for (const variable of variables) {
const existingDataVariable = existingDataVariables.find(
Expand All @@ -321,34 +328,92 @@ export class DocumentDataService {
} catch (err) {
console.log('Error in getVariablesByDtidAndDocType');
console.log(err);
return null;
return { variables: [], variableIds: [] };
}
}

/**
* Gets all document type provisions (combined with global provision info), a list of preselected
* provision ids, and any document_data_provisions and document_data_variables
* that have been saved for this combination of DTID and document_type_id.
*
* Returns
* @param document_type_id
* @param dtid
* @returns
*/
async getProvisionsByDocTypeIdAndDtid(document_type_id: number, dtid: number) {
try {
const documentData = await this.documentDataRepository
.createQueryBuilder('document_data')
.leftJoinAndSelect('document_data.document_data_provisions', 'document_data_provisions')
.leftJoinAndSelect('document_data_provisions.document_provision', 'provision')
.where('document_data.dtid = :dtid', { dtid })
.andWhere('document_data."documentTypeId" = :document_type_id', { document_type_id })
.getOne();
const documentData: DocumentData = await this.documentDataRepository.findOne({
where: { document_type: { id: document_type_id }, dtid: dtid },
});
const documentDataProvisions: DocumentDataProvision[] = await this.getDocumentDataProvisions(
document_type_id,
dtid
);
const documentDataVariables: DocumentDataVariable[] = await this.getDocumentDataVariables(document_type_id, dtid);
const mappedVariables = documentDataVariables.map((ddv) => {
return { id: ddv.id, variable_id: ddv.document_variable.id, saved_value: ddv.data_variable_value };
});
const variableIds: number[] = documentDataVariables.map((dataVariable) => dataVariable.document_variable.id);
const documentTypeProvisions: DocumentTypeProvision[] = await this.getDocumentTypeProvisions(document_type_id);

// saved provisions attached to the dtid
const existingDataProvisions: DocumentDataProvision[] = [];
if (documentData) existingDataProvisions.push(...documentData.document_data_provisions);
// all provisions with the specified document_type_id
const provisions: Provision[] = await this.provisionService.getAllProvisionsByDocTypeId(document_type_id);
const provisionIds = existingDataProvisions.map((dataProvision) => dataProvision.document_provision.id);
return { provisions, provisionIds };
const provisionIds = documentDataProvisions.map((dataProvision) => dataProvision.document_provision.id);
return {
provisions: documentTypeProvisions,
preselectedProvisionIds: provisionIds,
preselectedVariableIds: variableIds,
documentDataProvisions: documentDataProvisions,
savedVariableInfo: mappedVariables,
};
} catch (err) {
console.log('Error in getProvisionsByDocTypeIdAndDtid');
console.log(err);
return null;
}
}

// helper function for getProvisionsByDocTypeIdAndDtid
async getDocumentTypeProvisions(document_type_id: number): Promise<DocumentTypeProvision[]> {
const documentTypeProvisions: DocumentTypeProvision[] =
await this.provisionService.getDocumentTypeProvisionsByDocumentTypeId(document_type_id);
return documentTypeProvisions;
}

// helper function for getProvisionsByDocTypeIdAndDtid
async getDocumentDataProvisions(document_type_id: number, dtid: number): Promise<DocumentDataProvision[]> {
const documentData: DocumentData = await this.documentDataRepository.findOne({
where: { dtid: dtid, document_type: { id: document_type_id } },
relations: ['document_data_provisions'],
});
if (documentData && documentData.id) {
const documentDataProvisions: DocumentDataProvision[] = await this.documentDataProvisionRepository.find({
where: { document_data: { id: documentData.id } },
relations: ['document_type_provision', 'document_provision', 'document_data'],
});
return documentDataProvisions;
} else {
return [];
}
}

// helper function for getProvisionsByDocTypeIdAndDtid
async getDocumentDataVariables(document_type_id, dtid: number): Promise<DocumentDataVariable[]> {
const documentData: DocumentData = await this.documentDataRepository.findOne({
where: { dtid: dtid, document_type: { id: document_type_id } },
relations: ['document_data_provisions'],
});
if (documentData && documentData.id) {
const documentDataVariables: DocumentDataVariable[] = await this.documentDataVariableRepository.find({
where: { document_data: { id: documentData.id } },
relations: ['document_data', 'document_variable'],
});
return documentDataVariables;
} else {
return [];
}
}

async getEnabledProvisionsByDocTypeIdAndDtid(document_type_id: number, dtid: number) {
try {
const documentData: DocumentData = await this.documentDataRepository.findOne({
Expand Down
1 change: 0 additions & 1 deletion backend/src/document_data/dto/create-document_data.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { DocumentDataDto } from './document_data.dto';

export class CreateDocumentDataDto extends PickType(DocumentDataDto, [
'dtid',
'document_type_id',
'template_id',
'status',
'create_userid',
Expand Down
1 change: 0 additions & 1 deletion backend/src/document_data/dto/document_data.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export class DocumentDataDto {
dtid?: number;
document_type_id?: number;
template_id?: number;
status?: string;
enabled_provisions?: number[];
Expand Down
1 change: 0 additions & 1 deletion backend/src/document_data/dto/update-document_data.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { DocumentDataDto } from './document_data.dto';

export class UpdateDocumentDataDto extends PickType(DocumentDataDto, [
'dtid',
'document_type_id',
'template_id',
'status',
'enabled_provisions',
Expand Down
2 changes: 1 addition & 1 deletion backend/src/document_template/document_template.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class DocumentTemplateService {
.getMany();

if (!existingTemplates || existingTemplates.length === 0) {
return 0;
return 1;
} else {
let currentVersion = existingTemplates.reduce((max, item) => Math.max(max, item.template_version), 0);
return currentVersion + 1;
Expand Down
5 changes: 0 additions & 5 deletions backend/src/document_type/document_type.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ export class DocumentTypeController {
return this.documentTypeService.findAll();
}

@Get('get-group-max')
getGroupMax() {
return this.documentTypeService.getGroupMax();
}

@Get('get-group-max/:document_type_id')
getGroupMaxByDocTypeId(@Param('document_type_id') document_type_id: number) {
return this.documentTypeService.getGroupMaxByDocTypeId(document_type_id);
Expand Down
3 changes: 2 additions & 1 deletion backend/src/document_type/document_type.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ export class DocumentTypeService {
const provisionGroups = await this.provisionGroupRepository.find({
relations: ['document_type'],
});
return Array.from(provisionGroups).sort((a, b) => a.provision_group - b.provision_group);
console.log('pgs');
return provisionGroups.sort((a, b) => a.provision_group - b.provision_group);
}

async getGroupMaxByDocTypeId(document_type_id: number): Promise<any> {
Expand Down
Loading

0 comments on commit c3621bf

Please sign in to comment.