-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
14 changed files
with
745 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { test, describe, expect } from "vitest"; | ||
import { createDocument } from "./create"; | ||
import { getDocumentById } from "./get-by-id"; | ||
import { testConverter, type TestDoc, type TestModel } from "./test-model"; | ||
import type { FirestoreDataConverter } from "firebase-admin/firestore"; | ||
|
||
const testCollection = "create-test"; | ||
|
||
describe("Create a new document", () => { | ||
test("Should create a document success", async () => { | ||
const modelToSave: TestModel = { | ||
lastUpdate: new Date("2024-01-01"), | ||
name: "Test Document", | ||
}; | ||
const result = await createDocument( | ||
testCollection, | ||
testConverter, | ||
modelToSave, | ||
); | ||
expect(result).toMatchObject({ | ||
status: "success", | ||
data: expect.any(String), | ||
}); | ||
|
||
const getResult = await getDocumentById( | ||
testCollection, | ||
testConverter, | ||
result.data as string, | ||
); | ||
expect(getResult).toMatchObject({ | ||
status: "success", | ||
data: expect.objectContaining({ | ||
...modelToSave, | ||
id: result.data, | ||
}), | ||
}); | ||
}); | ||
|
||
test("Should return an error if id is present", async () => { | ||
const modelToSave: TestModel = { | ||
id: "test-id", | ||
lastUpdate: new Date("2024-01-01"), | ||
name: "Test Document", | ||
}; | ||
const result = await createDocument( | ||
testCollection, | ||
testConverter, | ||
modelToSave, | ||
); | ||
expect(result).toMatchObject({ | ||
status: "error", | ||
data: { | ||
code: `${testCollection}/create-error:id-is-present`, | ||
message: "Document ID is required", | ||
}, | ||
}); | ||
}); | ||
|
||
test("Should return an error if firestore error", async () => { | ||
const modelToSave: TestModel = { | ||
lastUpdate: new Date("2024-01-01"), | ||
name: "Test Document", | ||
}; | ||
const fakeConverter: FirestoreDataConverter<TestModel, TestDoc> = { | ||
toFirestore: (): TestDoc => { | ||
throw new Error("Error converting to firestore"); | ||
}, | ||
fromFirestore: (): TestModel => { | ||
throw new Error("Error converting from firestore"); | ||
}, | ||
}; | ||
const result = await createDocument( | ||
testCollection, | ||
fakeConverter, | ||
modelToSave, | ||
); | ||
expect(result).toMatchObject({ | ||
status: "error", | ||
data: { | ||
code: `${testCollection}/create-error`, | ||
message: `Error creating document in collection ${testCollection}`, | ||
}, | ||
}); | ||
}); | ||
|
||
test("Should return an error if id is present", async () => { | ||
const modelToSave: TestModel = { | ||
id: "test-id", | ||
lastUpdate: new Date("2024-01-01"), | ||
name: "Test Document", | ||
}; | ||
const result = await createDocument( | ||
testCollection, | ||
testConverter, | ||
modelToSave, | ||
); | ||
expect(result).toMatchObject({ | ||
status: "error", | ||
data: { | ||
code: `${testCollection}/create-error:id-is-present`, | ||
message: "Document ID is required", | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type { | ||
DocumentData, | ||
FirestoreDataConverter, | ||
} from "firebase-admin/firestore"; | ||
import { firestoreInstance } from "~/firebase/server"; | ||
import type { BaseModel } from "~/models/base-model"; | ||
import type { ServerResponse } from "~/models/server-response/server-response.type"; | ||
|
||
export const createDocument = async < | ||
M extends BaseModel, | ||
D extends DocumentData, | ||
C extends string, | ||
>( | ||
collection: C, | ||
converter: FirestoreDataConverter<M, D>, | ||
model: M, | ||
): Promise< | ||
ServerResponse< | ||
string, | ||
`${C}/create-error` | `${C}/create-error:id-is-present` | ||
> | ||
> => { | ||
try { | ||
if (model.id) { | ||
return { | ||
status: "error", | ||
data: { | ||
code: `${collection}/create-error:id-is-present`, | ||
message: "Document ID is required", | ||
}, | ||
}; | ||
} | ||
const result = await firestoreInstance | ||
.collection(collection) | ||
.withConverter<M, D>(converter) | ||
.add(model); | ||
return { | ||
status: "success", | ||
data: result.id, | ||
}; | ||
} catch (error) { | ||
console.error(`Error creating document in collection ${collection}`, error); | ||
return { | ||
status: "error", | ||
data: { | ||
code: `${collection}/create-error`, | ||
message: `Error creating document in collection ${collection}`, | ||
}, | ||
}; | ||
} | ||
}; |
Oops, something went wrong.