Skip to content

Commit

Permalink
refactor: prepare the migrate script
Browse files Browse the repository at this point in the history
  • Loading branch information
czabaj committed Oct 24, 2024
1 parent 52e5967 commit ae3f0ce
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 15 deletions.
2 changes: 1 addition & 1 deletion functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"serve": "npm run build && firebase emulators:start --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"test": "FIRESTORE_EMULATOR_HOST=\"127.0.0.1:9090\" jest",
"test": "FIRESTORE_EMULATOR_HOST=\"127.0.0.1:9090\" FIREBASE_AUTH_EMULATOR_HOST=\"127.0.0.1:9099\" jest",
"test:emulators": "firebase emulators:exec \"jest --watchAll\""
},
"source": "src/index.ts"
Expand Down
58 changes: 58 additions & 0 deletions functions/scripts/__tests__/migrate-places.test.ts
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,
});
});
29 changes: 29 additions & 0 deletions functions/scripts/migrate-places.ts
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);
}
3 changes: 3 additions & 0 deletions functions/scripts/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { migratePlaces } from "./migrate-places";

migratePlaces();
15 changes: 2 additions & 13 deletions functions/src/__tests__/online.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import * as path from "node:path";

import "jest";

import functions from "firebase-functions-test";
import * as admin from "firebase-admin";
import {
getFirestore,
Timestamp,
CollectionReference,
} from "firebase-admin/firestore";
import functions from "firebase-functions-test";

import * as myFunctions from "../index";
import { UserRole } from "../../../src/backend/UserRoles";
Expand All @@ -18,21 +15,13 @@ const testEnv = functions(
projectId: process.env.VITE_FIREBASE_PROJECT_ID,
storageBucket: process.env.VITE_FIREBASE_STORAGE_BUCKET,
},
path.join(__dirname, "../../../../beerbook2-da255-1c582faf4499.json")
path.join(__dirname, "../../../certs/beerbook2-da255-1c582faf4499.json")
);

afterAll(() => {
testEnv.cleanup();
});

// Make a fake document snapshot to pass to the function
const mock = testEnv.firestore.makeDocumentSnapshot(
{
text: "hello world",
},
"/lowercase/foo"
);

describe(`deletePlaceSubcollection`, () => {
const addPlace = async (opts: { placeId: string; withKegs: boolean }) => {
const db = getFirestore();
Expand Down
2 changes: 1 addition & 1 deletion functions/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"target": "es2020"
},
"compileOnSave": true,
"include": ["src"]
"include": ["src", "scripts"]
}

0 comments on commit ae3f0ce

Please sign in to comment.