-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'refs/heads/master' into dev-SCRUM-43
- Loading branch information
Showing
7 changed files
with
126 additions
and
23 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
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
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
File renamed without changes.
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
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,32 @@ | ||
import {describe, expect, test} from "vitest"; | ||
import {faker} from "@faker-js/faker"; | ||
import {UserPrefs, getUserPrefs, setUserPrefs} from "./user_prefs.ts"; | ||
import _ from "lodash"; | ||
|
||
|
||
describe("Firestore UserPrefs Tests", () => { | ||
test("Read/Write/Default Value Test", async () => { | ||
const user = { uid: faker.string.alphanumeric(20) } | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
const prefs = await getUserPrefs(user); | ||
|
||
expect(_.isEqual(prefs, UserPrefs.default()), "Non-existent UserPrefs should return default").toBeTruthy(); | ||
|
||
prefs.goal = 213; | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
await setUserPrefs(user, prefs); | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
const read_prefs = await getUserPrefs(user); | ||
|
||
console.log(prefs); | ||
console.log(read_prefs); | ||
|
||
expect(_.isEqual(read_prefs, prefs), "UserPref changes to be read").toBeTruthy(); | ||
}); | ||
}); |
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,43 @@ | ||
import {collection, doc, DocumentSnapshot, getDoc, setDoc, SnapshotOptions} from "firebase/firestore"; | ||
import {User} from "firebase/auth"; | ||
import {db} from "./firebase.ts"; | ||
|
||
|
||
export class UserPrefs { | ||
public goal: number; | ||
|
||
constructor(goal: number) { | ||
this.goal = goal; | ||
} | ||
|
||
static default(): UserPrefs { | ||
return new UserPrefs(100); | ||
} | ||
|
||
// Utility method for creating `Transactions` | ||
static fromFirestore(snapshot: DocumentSnapshot, options: SnapshotOptions): UserPrefs { | ||
const data = snapshot.data(options); | ||
if (!data) { | ||
throw Error("No data returned for snapshot!"); | ||
} | ||
return new UserPrefs(data.goal); | ||
} | ||
|
||
toSendObject(): object { | ||
const {...transObject} = this; | ||
return transObject; | ||
} | ||
} | ||
|
||
export async function getUserPrefs(user: User): Promise<UserPrefs> { | ||
const docRef = doc(collection(db, "UserPrefs"), user.uid); | ||
|
||
return await getDoc(docRef).then((ds) => | ||
UserPrefs.fromFirestore(ds, {}) | ||
).catch(() => UserPrefs.default()); | ||
} | ||
|
||
export async function setUserPrefs(user: User, prefs: UserPrefs): Promise<void> { | ||
const docRef = doc(collection(db, "UserPrefs"), user.uid); | ||
await setDoc(docRef, prefs.toSendObject()); | ||
} |