Skip to content

Commit

Permalink
feat: Add UsuarioDTO and ExameDTO interfaces; update Exame model for …
Browse files Browse the repository at this point in the history
…DTO integration; remove unused InMemoryRepository and AuthService; adjust repository exports and fix asset path in index.html
  • Loading branch information
werepa committed Dec 26, 2024
1 parent fe255e6 commit dfdc1ce
Show file tree
Hide file tree
Showing 26 changed files with 613 additions and 356 deletions.
4 changes: 2 additions & 2 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
{
"glob": "**/*",
"input": "public",
"src/assets"
"output": "src/assets"
}
],
"styles": ["src/global.scss", "src/theme/variables.scss"],
Expand Down Expand Up @@ -82,7 +82,7 @@
{
"glob": "**/*",
"input": "public",
"src/assets"
"output": "src/assets"
}
],
"styles": ["src/global.scss", "src/theme/variables.scss"],
Expand Down
145 changes: 0 additions & 145 deletions angular_old.json

This file was deleted.

81 changes: 81 additions & 0 deletions src/app/Repository/DatabaseRepository/DatabaseRepository.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { TestBed } from "@angular/core/testing"
import "firebase/firestore"
import { AngularFireModule } from "@angular/fire/compat"
import { AngularFirestoreCollection, AngularFirestoreModule } from "@angular/fire/compat/firestore"
import { DatabaseRepository } from "./DatabaseRepository"
import { firstValueFrom, map } from "rxjs"
import { ExameDTO } from "src/app/models/ExameDTO"
import { Exame, Material } from "src/app/models"

describe("DatabaseRepository", () => {
let component: DatabaseRepository
const firebaseConfig = {
apiKey: "AIzaSyDpMfldg10XJfdkap0lsQkfaFTzbVTv3Lo",
authDomain: "grade-anp-testes.firebaseapp.com",
projectId: "grade-anp-testes",
storageBucket: "grade-anp-testes.firebasestorage.app",
messagingSenderId: "486118580910",
appId: "1:486118580910:web:de3c6d36d5b1672d86c33f",
}

beforeEach(async () => {
TestBed.configureTestingModule({
imports: [AngularFireModule.initializeApp(firebaseConfig), AngularFirestoreModule],
providers: [DatabaseRepository, { provide: "EYEDROPS_REPOSITORY", useClass: DatabaseRepository }],
})
component = TestBed.inject(DatabaseRepository)
await truncate()
})

const truncate = async () => {
const eyedropsCollection: AngularFirestoreCollection<ExameDTO> = component.firestore.collection<ExameDTO>("eyedrops")
const actions = await firstValueFrom(eyedropsCollection.snapshotChanges())
for (let i = 0; i < actions.length; i++) {
const docId = actions[i].payload.doc.id
await component.firestore.collection("eyedrops").doc(docId).delete()
}
}

it("should access firebase and get no documents", async () => {
expect((await component.getAll()).length).toBe(0)
})

it("should create a document if not exists", async () => {
const material = Material.create({ numero: "123" })
await component.save(Exame.create({ material: material.toPersistence() }))
expect((await component.getAll()).length).toBe(1)
})

it("should update a document if exists", async () => {
const material = Material.create({ numero: "123" })
await component.save(Exame.create({ material: material.toPersistence() }))
await component.save(Exame.create({ material: material.toPersistence() }))
expect((await component.getAll()).length).toBe(1)
})

it("should get a document by codigo", async () => {
const material = Material.create({ numero: "123/24" })
await component.save(Exame.create({ material: material.toPersistence() }))
const exameDTO: ExameDTO = await component.getByCodigo("0123/2024 GO")
expect(exameDTO.material.codigo).toBe("0123/2024 GO")
})

it("should get a document by UF", async () => {
const material1 = Material.create({ numero: "123", uf: "GO" })
const material2 = Material.create({ numero: "124", uf: "go" })
const material3 = Material.create({ numero: "225", uf: "df" })
await component.save(Exame.create({ material: material1.toPersistence() }))
await component.save(Exame.create({ material: material2.toPersistence() }))
await component.save(Exame.create({ material: material3.toPersistence() }))
expect((await component.getByUF("GO")).length).toBe(2)
expect((await component.getByUF("df")).length).toBe(1)
})

// it("should dont update a blocked document by another user", async () => {
// await component.save(Exame.create(Material.create("123/24", "GO")))
// await component.save(Exame.create(Material.create("124/24", "go")))
// await component.save(Exame.create(Material.create("225/24", "df")))
// await component.block("123/24")
// expect((await component.getAll()).length).toBe(2)
// })
})
77 changes: 52 additions & 25 deletions src/app/Repository/DatabaseRepository/DatabaseRepository.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,49 @@
import { Injectable } from "@angular/core"
import { AngularFirestore, CollectionReference } from "@angular/fire/compat/firestore"
import { AngularFirestore, AngularFirestoreCollection, CollectionReference } from "@angular/fire/compat/firestore"
import { EyedropsRepository } from "../EyedropsRepository"
import { Exame } from "src/app/models"
import "firebase/compat/firestore"
import { ExameDTO } from "src/app/models/ExameDTO"
import { firstValueFrom, map } from "rxjs"

@Injectable({
providedIn: "root",
})
export class DatabaseRepository implements EyedropsRepository {
eyedropsCollection: CollectionReference
eyedropsCollection: AngularFirestoreCollection
eyedropsCollectionRef: CollectionReference

constructor(private firestore: AngularFirestore) {
this.eyedropsCollection = this.firestore.collection("eyedrops").ref
this.getByCodigo("0123/2024 GO")
.then((exame) => {
console.log("exame", exame)
})
.catch((error) => {
console.error("Error getting document: ", error)
})
constructor(public firestore: AngularFirestore) {
this.eyedropsCollection = this.firestore.collection("eyedrops")
this.eyedropsCollectionRef = this.firestore.collection("eyedrops").ref
}

async getAll(): Promise<ExameDTO[]> {
return firstValueFrom(
this.eyedropsCollection.snapshotChanges().pipe(
map((actions) =>
actions.map((a) => {
const data = a.payload.doc.data() as ExameDTO
const id = a.payload.doc.id
return { id, ...data }
})
)
)
)
}
async getByCodigo(codigo: string): Promise<any> {

async getByCodigo(codigo: string): Promise<ExameDTO> {
try {
const eyedropsRef = this.eyedropsCollection.where("codigo", "==", codigo)
const materialCollectionRef = this.firestore.collection("eyedrops/exame/material").ref
const eyedropsRef = materialCollectionRef.where("codigo", "==", codigo)
const eyedropsQuery = await eyedropsRef.limit(1).get()
if (eyedropsQuery.empty) return null
const exame: Exame = await eyedropsQuery.docs.map(async (doc: any) => {
const exame: ExameDTO = await eyedropsQuery.docs.map(async (doc: any) => {
const data: any = {
...doc.data(),
id: doc.id,
exame: doc.data(),
codigo: doc.data().codigo,
uf: doc.data().uf,
exame: doc.data().exame,
}
return data
})[0]
Expand All @@ -39,21 +53,34 @@ export class DatabaseRepository implements EyedropsRepository {
throw error
}
}
getByUF(codigo: string): Promise<any[]> {
throw new Error("Method not implemented.")

async getByUF(uf: string): Promise<ExameDTO[]> {
try {
const eyedropsRef = this.eyedropsCollectionRef.where("uf", "==", uf.toUpperCase())
const eyedropsQuery = await eyedropsRef.get()
if (eyedropsQuery.empty) return []
const exames: ExameDTO[] = eyedropsQuery.docs.map((doc: any) => {
const data: ExameDTO = {
...doc.data(),
id: doc.id,
}
return data
})
return exames
} catch (error) {
console.error(`Erro ao buscar documento uf:${uf}`, error)
throw error
}
}

async save(exame: Exame): Promise<void> {
console.log(exame.toPersistence())
try {
const query = await this.eyedropsCollection.where("codigo", "==", exame.material.codigo).get()
const query = await this.eyedropsCollectionRef.where("codigo", "==", exame.material.codigo).get()
if (query.empty) {
console.log("save - create", query.empty)
await this.eyedropsCollection.doc().set({ codigo: exame.material.codigo, exame: JSON.stringify(exame) })
await this.eyedropsCollection.doc().set(exame.toPersistence())
} else {
console.log("save - update", query.docs[0].id)
await this.eyedropsCollection
.doc(query.docs[0].id)
.set({ codigo: exame.material.codigo, exame: JSON.stringify(exame) })
await this.eyedropsCollection.doc(query.docs[0].id).set(exame.toPersistence())
}
} catch (error) {
console.error("Erro ao criar o documento:", error)
Expand Down
6 changes: 4 additions & 2 deletions src/app/Repository/EyedropsRepository.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Exame } from "../models"
import { ExameDTO } from "../models/ExameDTO"

export interface EyedropsRepository {
save(exame: Exame): Promise<void>
getByCodigo(codigo: string): Promise<any>
getByUF(codigo: string): Promise<any[]>
getAll(): Promise<ExameDTO[]>
getByCodigo(codigo: string): Promise<ExameDTO>
getByUF(codigo: string): Promise<ExameDTO[]>
truncate(): Promise<void>
}
12 changes: 0 additions & 12 deletions src/app/Repository/InMemoryRepository/InMemoryProvider.ts

This file was deleted.

Loading

0 comments on commit dfdc1ce

Please sign in to comment.