Skip to content

Commit

Permalink
#2004 - extended the summary to include average in Educational material
Browse files Browse the repository at this point in the history
  • Loading branch information
Brajesh Kumar authored and Brajesh Kumar committed Oct 6, 2023
1 parent 63150d5 commit a50741f
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@
>
</app-entity-subrecord>

<div
class="margin-top-large"
i18n="Total amount|Total amount of education material including a summary"
>
Total: {{ summary }}
<div class="margin-top-large">
<ng-container *ngFor="let title of summaryTitle">
<ng-container *ngIf="title.Total">
<span i18n="Total amount|Total amount of education material including a summary">
<strong>Total:</strong> {{ summary }} <br>
</span>
</ng-container>
<ng-container *ngIf="title.Average">
<span i18n="Average amount|Average amount of education material including a summary">
<strong>Average:</strong> {{ avgSummary }}
</span>
</ng-container>
</ng-container>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import { MockedTestingModule } from "../../../../utils/mocked-testing.module";
import { EducationalMaterial } from "../model/educational-material";
import { ConfigurableEnumValue } from "../../../../core/basic-datatypes/configurable-enum/configurable-enum.interface";
import { EntityMapperService } from "../../../../core/entity/entity-mapper/entity-mapper.service";
import { Subject } from "rxjs";
import { of, Subject } from "rxjs";
import { UpdatedEntity } from "../../../../core/entity/model/entity-update";
import { ActivatedRoute } from "@angular/router";
import { RouteData } from "app/core/config/dynamic-routing/view-config.interface";

describe("EducationalMaterialComponent", () => {
let component: EducationalMaterialComponent;
Expand All @@ -26,7 +28,33 @@ describe("EducationalMaterialComponent", () => {
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [EducationalMaterialComponent, MockedTestingModule.withState()],
providers: [
{
provide: ActivatedRoute,
useValue: {
data: of({
config: {
entity: "Child",
panels: [
{
title: "Educational Materials",
summary: [
{
Total: "Total",
},
{
Average: "Average",
},
],
},
],
},
}),
},
},
],
}).compileComponents();

const entityMapper = TestBed.inject(EntityMapperService);
spyOn(entityMapper, "receiveUpdates").and.returnValue(updates);
}));
Expand Down Expand Up @@ -95,6 +123,36 @@ describe("EducationalMaterialComponent", () => {
const newRecord = component.newRecordFactory();
expect(newRecord.child).toBe(child.getId());
});
it("produces an empty summary when there are no records", () => {
component.records = [];
component.updateSummary();
expect(component.summary).toHaveSize(0);
});

it("should set summaryTitle based on panel title", () => {
setRecordsAndGenerateSummary([{ Total: "Total" }, { Average: "Average" }])

const routeData: RouteData = {
config: {
panels: [
{
title: "Educational Materials",
summary: [
{
Total: "Total",
},
{
Average: "Average",
},
],
},
],
},
};

component.getSummaryList();
expect(component.summaryTitle).toEqual([{ Total: "Total" }, { Average: "Average" }]);
});

it("should update the summary when entity updates are received", async () => {
const update1 = EducationalMaterial.create({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Component, Input, OnInit } from "@angular/core";
import { NgFor, NgIf, } from "@angular/common";
import { EducationalMaterial } from "../model/educational-material";
import { Child } from "../../model/child";
import { FormFieldConfig } from "../../../../core/common-components/entity-form/entity-form/FormConfig";
Expand All @@ -8,6 +9,9 @@ import { applyUpdate } from "../../../../core/entity/model/entity-update";
import { filter } from "rxjs/operators";
import { UntilDestroy, untilDestroyed } from "@ngneat/until-destroy";
import { EntitySubrecordComponent } from "../../../../core/common-components/entity-subrecord/entity-subrecord/entity-subrecord.component";
import { EntityListConfig } from "app/core/entity-list/EntityListConfig";
import { ActivatedRoute } from "@angular/router";
import { RouteData } from "app/core/config/dynamic-routing/view-config.interface";

/**
* Displays educational materials of a child, such as a pencil, rulers, e.t.c
Expand All @@ -18,13 +22,16 @@ import { EntitySubrecordComponent } from "../../../../core/common-components/ent
@Component({
selector: "app-educational-material",
templateUrl: "./educational-material.component.html",
imports: [EntitySubrecordComponent],
imports: [EntitySubrecordComponent,NgIf,NgFor],
standalone: true,
})
export class EducationalMaterialComponent implements OnInit {
@Input() entity: Child;
records: EducationalMaterial[] = [];
summary = "";
avgSummary = "";
summaryTitle: { [key: string]: string }[]
listConfig: EntityListConfig;

@Input() config: { columns: FormFieldConfig[] } = {
columns: [
Expand All @@ -35,7 +42,8 @@ export class EducationalMaterialComponent implements OnInit {
],
};

constructor(private entityMapper: EntityMapperService) {
constructor(private entityMapper: EntityMapperService,
private route: ActivatedRoute ) {
this.entityMapper
.receiveUpdates(EducationalMaterial)
.pipe(
Expand Down Expand Up @@ -65,6 +73,7 @@ export class EducationalMaterialComponent implements OnInit {
(mat) => mat.child === this.entity.getId(),
);
this.updateSummary();

}

newRecordFactory = () => {
Expand All @@ -83,15 +92,49 @@ export class EducationalMaterialComponent implements OnInit {
* human-readable format
*/
updateSummary() {
const summary = new Map<string, number>();
this.records.forEach((m) => {
const previousValue = summary.has(m.materialType.label)
? summary.get(m.materialType.label)
: 0;
summary.set(m.materialType.label, previousValue + m.materialAmount);
});
this.summary = [...summary]
.map(([key, value]) => key + ": " + value)
.join(", ");
const summary = new Map<string, { count: number; sum: number }>();
const average = new Map<string, number>();

// Initialize summary and average maps in a single loop
for (const m of this.records) {
if (m.materialType) {
const label = m.materialType.label;
const amount = m.materialAmount;

if (!summary.has(label)) {
summary.set(label, { count: 0, sum: 0 });
}

const labelData = summary.get(label);
labelData.count++;
labelData.sum += amount;
}
}

// Calculate averages and build summary strings
const summaryArray: string[] = [];
const avgSummaryArray: string[] = [];

for (const [label, labelData] of summary.entries()) {
const avg = labelData.sum / labelData.count;
average.set(label, avg);

summaryArray.push(`${label}: ${labelData.sum}`);
avgSummaryArray.push(`${label}: ${avg}`);
}

this.summary = summaryArray.join(", ");
this.avgSummary = avgSummaryArray.join(", ");
this.getSummaryList();
}


getSummaryList(){
this.route.data.subscribe(
(data: RouteData<EntityListConfig>) => (this.listConfig = data.config),
);
this.summaryTitle = this.listConfig['panels']
.find((panel: { title: string })=> panel.title === "Educational Materials").summary;

}
}
9 changes: 9 additions & 0 deletions src/app/core/config/config-fix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,15 @@ export const defaultJsonConfig = {
"title": "",
"component": "EducationalMaterial"
}
],
"summary": [
{
"Total": $localize `:Total item Count:Total`
},

{
"Average": $localize `:Average Per Item:Average`
}
]
},
{
Expand Down

0 comments on commit a50741f

Please sign in to comment.