From 958e2a7978bf6e8fc7a4f0a918a69d3395696ebd Mon Sep 17 00:00:00 2001 From: Austin Turner Date: Thu, 8 Feb 2024 20:44:16 -0700 Subject: [PATCH] Fix user profile fetch for local user --- apps/api/src/app/controllers/sf-query.controller.ts | 4 ++-- apps/api/src/app/controllers/user.controller.ts | 9 ++++++++- libs/api-config/src/lib/env-config.ts | 12 +++++++++++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/apps/api/src/app/controllers/sf-query.controller.ts b/apps/api/src/app/controllers/sf-query.controller.ts index 297594b65..1f60b9fc8 100644 --- a/apps/api/src/app/controllers/sf-query.controller.ts +++ b/apps/api/src/app/controllers/sf-query.controller.ts @@ -1,9 +1,9 @@ import { NextFunction, Request, Response } from 'express'; +import { body, query as queryString } from 'express-validator'; import * as jsforce from 'jsforce'; import * as queryService from '../services/query'; -import { sendJson } from '../utils/response.handlers'; import { UserFacingError } from '../utils/error-handler'; -import { body, query as queryString } from 'express-validator'; +import { sendJson } from '../utils/response.handlers'; export const routeValidators = { query: [body('query').isString()], diff --git a/apps/api/src/app/controllers/user.controller.ts b/apps/api/src/app/controllers/user.controller.ts index 577e9aa84..4655d874e 100644 --- a/apps/api/src/app/controllers/user.controller.ts +++ b/apps/api/src/app/controllers/user.controller.ts @@ -1,4 +1,4 @@ -import { logger, mailgun } from '@jetstream/api-config'; +import { ENV, logger, mailgun } from '@jetstream/api-config'; import { UserProfileAuth0Ui, UserProfileServer, UserProfileUi, UserProfileUiWithIdentities } from '@jetstream/types'; import { AxiosError } from 'axios'; import * as express from 'express'; @@ -60,6 +60,13 @@ export async function emailSupport(req: express.Request, res: express.Response) export async function getUserProfile(req: express.Request, res: express.Response) { const auth0User = req.user as UserProfileServer; + + // use fallback locally and on CI + if (ENV.EXAMPLE_USER_OVERRIDE && ENV.EXAMPLE_USER_PROFILE && req.hostname === 'localhost') { + sendJson(res, ENV.EXAMPLE_USER_PROFILE); + return; + } + const user = await userDbService.findByUserId(auth0User.id); if (!user) { throw new UserFacingError('User not found'); diff --git a/libs/api-config/src/lib/env-config.ts b/libs/api-config/src/lib/env-config.ts index 50f96cb80..4a549a848 100644 --- a/libs/api-config/src/lib/env-config.ts +++ b/libs/api-config/src/lib/env-config.ts @@ -1,5 +1,5 @@ import { ensureBoolean, ensureStringValue } from '@jetstream/shared/utils'; -import { UserProfileServer } from '@jetstream/types'; +import { UserProfileServer, UserProfileUi } from '@jetstream/types'; import * as dotenv from 'dotenv'; import { readFileSync } from 'fs-extra'; import { join } from 'path'; @@ -41,11 +41,21 @@ const EXAMPLE_USER: UserProfileServer = { user_id: 'EXAMPLE_USER', }; +const EXAMPLE_USER_PROFILE: UserProfileUi = { + ...EXAMPLE_USER._json, + id: 'EXAMPLE_USER', + userId: 'EXAMPLE_USER', + createdAt: new Date().toISOString(), + updatedAt: new Date().toISOString(), + preferences: { skipFrontdoorLogin: false }, +}; + export const ENV = { IS_CI: ensureBoolean(process.env.CI), // LOCAL OVERRIDE EXAMPLE_USER_OVERRIDE: ensureBoolean(process.env.EXAMPLE_USER_OVERRIDE), EXAMPLE_USER: process.env.EXAMPLE_USER_OVERRIDE ? EXAMPLE_USER : null, + EXAMPLE_USER_PROFILE: process.env.EXAMPLE_USER_OVERRIDE ? EXAMPLE_USER_PROFILE : null, // SYSTEM NODE_ENV: process.env.NODE_ENV, ENVIRONMENT: process.env.ENVIRONMENT || 'production',