Skip to content

Commit

Permalink
BO16: Refactor partnership retrieval and converter
Browse files Browse the repository at this point in the history
  • Loading branch information
bonfry committed Jul 31, 2024
1 parent 77f8e02 commit 69ec61d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/data/firestore/partenership/get-partnership-paginated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import type { Partnership } from "~/models/partnership/partnership.type";
import getDocsPaginated from "../common/get-docs-paginated";
import { partnerDataConverter, partnershipDataConverter } from "./models";
import { getFirestore } from "firebase-admin/firestore";
import { firestoreInstance } from "~/firebase/server";

const collection = "partnerships" as const;


const getPartnershipsPaginated = async (params: PaginationParams<Partnership>) => {
const result = await getDocsPaginated(collection, partnershipDataConverter, params);
const db = getFirestore();

if (result.status == "success") {
result.data.data = await Promise.all(
result.data.data.map(async (p) => {
const snapshot = await db.collection(`${collection}/${p.id}/partners`)
const snapshot = await firestoreInstance.collection(`${collection}/${p.id}/partners`)
.withConverter(partnerDataConverter)
.orderBy("position", "asc")
.get();
Expand Down
9 changes: 5 additions & 4 deletions src/data/firestore/partenership/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ type PartnershipDoc = FirestoreDocument<Partnership>;
type PartnerDoc = FirestoreDocument<Partner>;

const partnerDataConverter: FirestoreDataConverter<Partner, PartnerDoc> = {
toFirestore: (model: Partner) => {
delete model.id;
return { ...model };
},

fromFirestore: (snapshot: QueryDocumentSnapshot) => {
const data = snapshot.data() as PartnerDoc;
data.id = snapshot.id;

return { ...data };
},
toFirestore: (model: Partner) => {
delete model.id;
return { ...model };
}
}

Expand Down

0 comments on commit 69ec61d

Please sign in to comment.