-
Notifications
You must be signed in to change notification settings - Fork 0
/
firestore-handler.ts
35 lines (33 loc) · 1.22 KB
/
firestore-handler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import {Firestore} from "@google-cloud/firestore";
import {CheckDataType, LocalMetadataType} from "./types";
import {DocumentProcessorServiceClient} from "@google-cloud/documentai";
import {FileMetadata} from "@google-cloud/storage";
import {CheckHandler} from "./abstract-check-handler";
export class FirestoreSaveHandler extends CheckHandler {
fireStoreCollection = process.env.FIRESTORE_COLLECTION;
firestore = new Firestore({
projectId: process.env.PROJECT_ID,
});
async handle(
fileMetaData: LocalMetadataType,
documentaiClient: DocumentProcessorServiceClient,
checkData: CheckDataType
) {
console.log("Saving to Firestore");
checkData = this.replaceUndefinedWithNull(checkData);
const docRef = this.firestore
.collection(this.fireStoreCollection)
.doc(checkData.id);
checkData = this.replaceUndefinedWithNull(checkData);
await docRef.set(checkData);
return super.handle(fileMetaData, documentaiClient, checkData);
}
private replaceUndefinedWithNull(checkData: CheckDataType): CheckDataType {
return Object.fromEntries(
Object.entries(checkData).map(([key, value]) => [
key,
value === undefined ? null : value,
])
) as CheckDataType;
}
}