diff --git a/functions/scripts/runtimeConfig/model.ts b/functions/scripts/runtimeConfig/model.ts index 420400fbaa..74ebc7eb2f 100644 --- a/functions/scripts/runtimeConfig/model.ts +++ b/functions/scripts/runtimeConfig/model.ts @@ -21,4 +21,7 @@ export const runtimeConfigTest: configVars = { prerender: { api_key: 'fake_prerender_key', }, + migration: { + api_key: 'fake_migration_key', + }, } diff --git a/functions/src/Utils/migration.utils.ts b/functions/src/Utils/migration.utils.ts new file mode 100644 index 0000000000..1dc9e60ac5 --- /dev/null +++ b/functions/src/Utils/migration.utils.ts @@ -0,0 +1,31 @@ +import { CONFIG } from '../config/config' + +export function migrationEnabled() { + switch (CONFIG.deployment.site_url) { + case 'https://dev.onearmy.world': + case 'https://dev.community.projectkamp.com': + case 'https://dev.community.fixing.fashion': + return true + default: + return false + } +} + +export function migrationProject() { + switch (CONFIG.deployment.site_url) { + case 'https://dev.onearmy.world': + return 'dev_pp' + case 'https://community.preciousplastic.com': + return 'prod_pp' + case 'https://dev.community.projectkamp.com': + return 'dev_pk' + case 'https://community.projectkamp.com': + return 'prod_pk' + case 'https://dev.community.fixing.fashion': + return 'dev_ff' + case 'https://community.fixing.fashion': + return 'prod_ff' + default: + return null + } +} diff --git a/functions/src/config/config.ts b/functions/src/config/config.ts index 1722306ea6..0eaf800b43 100644 --- a/functions/src/config/config.ts +++ b/functions/src/config/config.ts @@ -21,6 +21,9 @@ if (Object.keys(c).length === 0) { slack_webhook: 'http://simulated-webhook-receiver:30102/slack', }, service: null, + migration: { + api_key: 'fake_key', + }, } as any } else { c = { diff --git a/functions/src/index.ts b/functions/src/index.ts index 2aff161dd8..206d8fc41e 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -17,6 +17,8 @@ exports.aggregations = require('./aggregations') exports.database = require('./database') +exports.supabaseCreateAuthUser = require('./supabaseMigration') + exports.userUpdates = UserUpdates.handleUserUpdates exports.discussionUpdates = DiscussionUpdates.handleDiscussionUpdate diff --git a/functions/src/supabaseMigration/createAuthUser.ts b/functions/src/supabaseMigration/createAuthUser.ts new file mode 100644 index 0000000000..384b5ed4eb --- /dev/null +++ b/functions/src/supabaseMigration/createAuthUser.ts @@ -0,0 +1,42 @@ +// Import Firebase Functions and Admin SDK +import * as functions from 'firebase-functions' +import axios from 'axios' +import { migrationEnabled, migrationProject } from '../Utils/migration.utils' +import { CONFIG } from '../config/config' + +const MIGRATION_API = 'https://supabase-migration-api.fly.dev/migration' +const MIGRATION_API_KEY = CONFIG.migration.api_key + +async function sendToSupabase(userDoc) { + try { + const response = await axios.post(`${MIGRATION_API}/auth`, userDoc, { + headers: { + 'x-api-key': MIGRATION_API_KEY, + }, + }) + + console.log('User synced with Supabase') + // console.log('User synced with Supabase:', response.data) + // return response.data + } catch (error) { + console.error( + 'Error syncing user with Supabase:', + error.response?.data || error.message, + ) + throw new Error('Failed to sync user with Supabase') + } +} + +// Firebase function triggered on user creation +exports.default = functions.auth.user().onCreate((user) => { + if (!migrationEnabled) return + const userDoc = { + uid: user.uid, + project: migrationProject, + email: user.email, + doc: user, + } + console.log('Sending new user to supabase') + // console.log('Sending new user to supabase:', JSON.stringify(userDoc)) + return sendToSupabase(userDoc) +}) diff --git a/functions/src/supabaseMigration/index.ts b/functions/src/supabaseMigration/index.ts new file mode 100644 index 0000000000..a2abc6c1bd --- /dev/null +++ b/functions/src/supabaseMigration/index.ts @@ -0,0 +1 @@ +exports.createAuthUser = require('./createAuthUser') diff --git a/shared/models/config.ts b/shared/models/config.ts index 283e9bdf51..cea6f04783 100644 --- a/shared/models/config.ts +++ b/shared/models/config.ts @@ -34,4 +34,7 @@ export interface configVars { prerender: { api_key: string } + migration?: { + api_key: string + } }