-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add user database tests (#6)
- Loading branch information
1 parent
dd84d90
commit 17d2c03
Showing
8 changed files
with
74 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,41 @@ | ||
import {Firestore} from "@google-cloud/firestore"; | ||
import { User } from "src/models/User"; | ||
|
||
export class Database { | ||
db: Firestore; | ||
private db: Firestore; | ||
|
||
constructor() { | ||
this.db = new Firestore({ | ||
projectId: "birds-of-paradise", | ||
// keyFilename: '/path/to/keyfile.json', | ||
}); | ||
} | ||
} | ||
|
||
|
||
async setUser({userId} :any): Promise<any> { | ||
const userDoc = this.db.collection('users').doc(userId); | ||
|
||
return userDoc.set({ | ||
userId, | ||
}, {merge: true}); | ||
} | ||
|
||
async getUser({userId}: any): Promise<User> { | ||
const userDoc = this.db.collection('users').doc(userId); | ||
const snapshot = await userDoc.get(); | ||
const completedMissions = snapshot.data()?.completedMissions || []; | ||
|
||
return { userId, completedMissions } | ||
} | ||
|
||
async addCompletedMission({userId, missionId} :any): Promise<any> { | ||
const userDoc = this.db.collection('users').doc(userId); | ||
const { completedMissions } = await this.getUser({userId}); | ||
const updatedMissions = [ ...completedMissions, missionId ] | ||
|
||
|
||
return userDoc.set({ | ||
completedMissions: updatedMissions, | ||
}, {merge: true}); | ||
} | ||
} |
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,4 @@ | ||
export class LearningResource { | ||
title!: string; | ||
link!: string; | ||
} |
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,11 @@ | ||
import { LearningResource } from "./LearningResource"; | ||
|
||
export class Mission { | ||
id!: string; | ||
title!: string; | ||
technologies!: string[]; | ||
learningResources!: LearningResource[]; | ||
|
||
// status is not stored in the database, it is calculated from the user object | ||
status!: string; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
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 |
---|---|---|
@@ -1,15 +1,4 @@ | ||
// TODO | ||
export type Email = string; | ||
export type Timestamp = number; | ||
export type SessionId = string; | ||
export type SessionResult = object | SessionId; | ||
|
||
export class User { | ||
userId?: string; | ||
|
||
email?: Email; | ||
|
||
results?: SessionResult[]; | ||
|
||
lastSession?: Timestamp; | ||
} | ||
userId!: string; | ||
completedMissions!: string[]; | ||
} |