Skip to content

Commit

Permalink
feat: Update EyedropsRepository to use 'uf' parameter; enhance MainPa…
Browse files Browse the repository at this point in the history
…ge and ExameService for improved user and exam management; add tests for user login and exam retrieval
  • Loading branch information
werepa committed Dec 27, 2024
1 parent f3a0723 commit a31b156
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/app/Repository/EyedropsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export interface EyedropsRepository {
save(exame: Exame): Promise<void>
getAll(): Promise<ExameDTO[]>
getByCodigo(codigo: string): Promise<ExameDTO>
getByUF(codigo: string): Promise<ExameDTO[]>
getByUF(uf: string): Promise<ExameDTO[]>
truncate(): Promise<void>
}
35 changes: 31 additions & 4 deletions src/app/main/main.page.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ComponentFixture, TestBed } from "@angular/core/testing"
import { MainPage } from "./main.page"
import { STEP, TAREFAS } from "../models/listas"
import { Exame, Material, Usuario } from "../models"
import { Exame, ExameDTO, Material, Usuario } from "../models"
import { ExameService, ExameState } from "../services/exame.service"

describe("MainPage", () => {
fdescribe("MainPage", () => {
let component: MainPage
let fixture: ComponentFixture<MainPage>
let exame: Exame
Expand All @@ -13,7 +13,17 @@ describe("MainPage", () => {
beforeEach(async () => {
fixture = await TestBed.configureTestingModule({
imports: [MainPage],
providers: [ExameService, { provide: "EYEDROPS_REPOSITORY", useClass: class {} }],
providers: [
ExameService,
{
provide: "EYEDROPS_REPOSITORY",
useClass: class {
getByUF(): Promise<ExameDTO[]> {
return Promise.resolve([])
}
},
},
],
}).compileComponents()
fixture = TestBed.createComponent(MainPage)
component = fixture.componentInstance
Expand All @@ -24,7 +34,7 @@ describe("MainPage", () => {
uf: "GO",
perfil: "Perito",
})
component.onChangeUsuarioAtual(usuarioAtual)
await component.onChangeUsuarioAtual(usuarioAtual)
const material = Material.create({ numero: "1" })
exame = Exame.create({ material: material.toPersistence() })
})
Expand Down Expand Up @@ -99,6 +109,7 @@ describe("MainPage", () => {
expect(component.state().listaExames.length).toBe(0)
const listaExames = component.state().listaExames
listaExames.push(exame)
component.exameService.listaExamesInicial.push(exame)
component.state.update((s) => ({ ...s, listaExames }))
expect(component.state().listaExames.length).toBe(1)
populateMaterialFields()
Expand Down Expand Up @@ -1253,4 +1264,20 @@ describe("MainPage", () => {
component.state().exameAtual.checkIsFinished()
expect(component.state().exameAtual.currentStep).toBe(STEP.TAREFAS_CONCLUIDAS)
})

fit("should update state().listaExames after onChangeUsuarioAtual", async () => {
const exameDTO1 = { material: Material.create({ numero: "123/2024" }).toPersistence() }
const exameDTO2 = { material: Material.create({ numero: "124/2024" }).toPersistence() }
const exameDTO3 = { material: Material.create({ numero: "125/2024" }).toPersistence() }
const exame1 = Exame.create(exameDTO1)
const exame2 = Exame.create(exameDTO2)
const exame3 = Exame.create(exameDTO3)
expect(component.state().listaExames.length).toBe(0)
const usuarioAtual = Usuario.create({ codigo: "0000", nome: "Castro", perfil: "Perito", uf: "GO" })
spyOn(component.exameService.repository, "getByUF").and.returnValue(
Promise.resolve([exame1, exame2, exame3].map((exame) => exame.toPersistence()))
)
await component.onChangeUsuarioAtual(usuarioAtual)
expect(component.state().listaExames.length).toBe(3)
})
})
20 changes: 12 additions & 8 deletions src/app/main/main.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class MainPage implements OnInit {
state: WritableSignal<ExameState>
listaPessoas: Usuario[] = this.exameService.getListaPessoas()

constructor(private exameService: ExameService, private fb: FormBuilder, private breakpointObserver: BreakpointObserver) {
constructor(public exameService: ExameService, private fb: FormBuilder, private breakpointObserver: BreakpointObserver) {
this.state = this.exameService.state
addIcons({ camera, cameraOutline, checkmarkOutline, trashOutline, addOutline, printOutline, syncOutline })
}
Expand Down Expand Up @@ -101,6 +101,10 @@ export class MainPage implements OnInit {
this.mostrarBateria = false
}

async saveExame(exame: Exame) {
await this.exameService.saveExame(exame)
}

//sincroniza dados com Firebase
syncWithFirebase() {
this.exameService.syncWithFirebase()
Expand Down Expand Up @@ -176,19 +180,18 @@ export class MainPage implements OnInit {
if (this.state().exameAtual.checkIsFinished()) this.mostrarTarefasConcluidas = true
}

onChangeUsuarioAtual(usuario: Usuario) {
this.exameService.changeUsuarioAtual(usuario)
// this.state.update((s) => ({ ...s, usuarioAtual: usuario }))
async onChangeUsuarioAtual(usuario: Usuario) {
await this.exameService.changeUsuarioAtual(usuario)
}

onChangeExameAtual(exame: Exame) {
this.state.update((s) => ({ ...s, exameAtual: exame }))
this.exameService.changeExameAtual(exame)
}

onChangeMaterialAtual(material: Material) {
const ultExame = this.state().exameAtual
const exame = this.getExame(material)
this.state.update((s) => ({ ...s, exameAtual: exame }))
this.exameService.changeExameAtual(exame)
if (ultExame === exame) this.tabAtual = "fluxo"
if (ultExame === exame) this.selectedTab = "fluxo"
}
Expand All @@ -213,6 +216,7 @@ export class MainPage implements OnInit {
if (!exame) {
exame = Exame.create({ material: material.toPersistence() })
listaExames.push(exame)
this.exameService.listaExamesInicial.push(exame)
}
if (!this.state().exameAtual) this.state.update((s) => ({ ...s, exameAtual: exame }))
return exame
Expand Down Expand Up @@ -748,8 +752,8 @@ export class MainPage implements OnInit {
return usuarioAtual ? usuarioAtual.nome : "Selecione o responsável"
}

setUsuarioAtual(usuario: Usuario) {
this.onChangeUsuarioAtual(usuario)
async setUsuarioAtual(usuario: Usuario) {
await this.onChangeUsuarioAtual(usuario)
this.setModalPessoasOpen(false)
const materialArray = this.form.get("materiais") as FormArray
if (!materialArray.controls.length) this.addMaterialControl()
Expand Down
14 changes: 13 additions & 1 deletion src/app/services/exame.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DatabaseRepository } from "../Repository"
import "firebase/firestore"
import { AngularFireModule } from "@angular/fire/compat"
import { AngularFirestoreModule } from "@angular/fire/compat/firestore"
import { Exame, Material, STEP } from "../models"
import { Exame, Material, STEP, Usuario } from "../models"

describe("ExameService", () => {
let service: ExameService
Expand All @@ -26,6 +26,18 @@ describe("ExameService", () => {
await service.repository.truncate()
})

it("should get documents after user login", async () => {
service.repository.save(Exame.create({ material: Material.create({ numero: "123/2024" }).toPersistence() }))
service.repository.save(Exame.create({ material: Material.create({ numero: "124/2024" }).toPersistence() }))
service.repository.save(Exame.create({ material: Material.create({ numero: "125/2024" }).toPersistence() }))
service.repository.save(Exame.create({ material: Material.create({ numero: "26/2024", uf: "DF" }).toPersistence() }))
service.repository.save(Exame.create({ material: Material.create({ numero: "27/2024", uf: "DF" }).toPersistence() }))
expect(service.state().listaExames.length).toBe(0)
const usuarioAtual = Usuario.create({ codigo: "0000", nome: "Castro", perfil: "Perito", uf: "GO" })
await service.changeUsuarioAtual(usuarioAtual)
expect(service.state().listaExames.length).toBe(3)
})

it("should list differences between two exameDTO", () => {
const exame1 = Exame.create({ material: Material.create({ numero: "123/2024" }).toPersistence() }).toPersistence()
const lastUpdate = new Date(new Date().getTime() + 10000)
Expand Down
26 changes: 24 additions & 2 deletions src/app/services/exame.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class ExameService {
usuarioAtual: null,
exameAtual: null,
})
exameInicial: ExameState = { ...this.state() }
listaExamesInicial: Exame[] = []

constructor(@Inject("EYEDROPS_REPOSITORY") public repository: EyedropsRepository) {}

Expand Down Expand Up @@ -79,11 +79,33 @@ export class ExameService {
return listaPessoas
}

changeUsuarioAtual(usuario: Usuario) {
async saveExame(exame: Exame) {
await this.repository.save(exame)
this.state.update((state) => {
state.listaExames = [...state.listaExames, exame]
return state
})
}

async refreshListaExames(): Promise<void> {
const uf = this.state().usuarioAtual?.uf
const examesDTO = await this.repository.getByUF(uf)
const exames = []
for (const exameDTO of examesDTO) {
exames.push(Exame.create(exameDTO))
}
this.state.update((state) => {
state.listaExames = [...exames]
return state
})
}

async changeUsuarioAtual(usuario: Usuario) {
this.state.update((state) => {
state.usuarioAtual = usuario
return state
})
await this.refreshListaExames()
}

changeExameAtual(exame: Exame) {
Expand Down

0 comments on commit a31b156

Please sign in to comment.