-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: prepare the migrate script
- Loading branch information
Showing
6 changed files
with
94 additions
and
15 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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import * as path from "node:path"; | ||
|
||
import { getFirestore } from "firebase-admin/firestore"; | ||
import { https } from "firebase-functions"; | ||
import functions from "firebase-functions-test"; | ||
|
||
import { place } from "../../../src/backend/FirestoreModels.gen"; | ||
import { NotificationEvent } from "../../../src/backend/NotificationEvents"; | ||
import { UserRole } from "../../../src/backend/UserRoles"; | ||
import { migratePlaces } from "../migrate-places"; | ||
|
||
const testEnv = functions( | ||
{ | ||
projectId: process.env.VITE_FIREBASE_PROJECT_ID, | ||
storageBucket: process.env.VITE_FIREBASE_STORAGE_BUCKET, | ||
}, | ||
path.join(__dirname, "../../../certs/beerbook2-da255-1c582faf4499.json") | ||
); | ||
|
||
afterAll(() => { | ||
testEnv.cleanup(); | ||
}); | ||
|
||
const addPlace = async (opts: { placeId: string; users: place["users"] }) => { | ||
const db = getFirestore(); | ||
const placeCollection = db.collection("places"); | ||
const placeDoc = placeCollection.doc(opts.placeId); | ||
await placeDoc.set({ | ||
users: opts.users, | ||
}); | ||
return placeDoc; | ||
}; | ||
|
||
const migratePlaceFn = https.onCall(async () => { | ||
await migratePlaces(); | ||
}); | ||
|
||
test(`migratePlaces`, async () => { | ||
const users = { | ||
owner: UserRole.owner, | ||
admin: UserRole.admin, | ||
}; | ||
const placeSeed = { | ||
placeId: "test-place", | ||
users, | ||
}; | ||
const placeDoc = await addPlace(placeSeed); | ||
const wrapped = testEnv.wrap(migratePlaceFn); | ||
await wrapped({}); | ||
const actualDocSnap = await placeDoc.get(); | ||
expect(actualDocSnap.data()).toEqual({ | ||
accounts: { | ||
owner: [UserRole.owner, NotificationEvent.unsubscribed], | ||
admin: [UserRole.admin, NotificationEvent.unsubscribed], | ||
}, | ||
users, | ||
}); | ||
}); |
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,29 @@ | ||
import { initializeApp } from "firebase-admin/app"; | ||
import { getFirestore } from "firebase-admin/firestore"; | ||
|
||
import { place } from "../../src/backend/FirestoreModels.gen"; | ||
import { NotificationEvent } from "../../src/backend/NotificationEvents"; | ||
|
||
const app = initializeApp(); | ||
|
||
export async function migratePlaces() { | ||
const db = getFirestore(app); | ||
const places = db.collection("places"); | ||
const placesSnap = await places.get(); | ||
const promises: Promise<any>[] = []; | ||
placesSnap.docs.forEach((placeDoc) => { | ||
const placeData = placeDoc.data() as place; | ||
const accounts = {} as place["accounts"]; | ||
if (placeData.users) { | ||
for (const [uuid, role] of Object.entries(placeData.users)) { | ||
accounts[uuid] = [role, NotificationEvent.unsubscribed]; | ||
} | ||
} | ||
promises.push( | ||
placeDoc.ref.update({ | ||
accounts, | ||
}) | ||
); | ||
}); | ||
return Promise.allSettled(promises); | ||
} |
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,3 @@ | ||
import { migratePlaces } from "./migrate-places"; | ||
|
||
migratePlaces(); |
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 |
---|---|---|
|
@@ -12,5 +12,5 @@ | |
"target": "es2020" | ||
}, | ||
"compileOnSave": true, | ||
"include": ["src"] | ||
"include": ["src", "scripts"] | ||
} |