diff --git a/src/entity/dataset/utils.ts b/src/entity/dataset/utils.ts index c42b9d49c5..e0c1177d15 100644 --- a/src/entity/dataset/utils.ts +++ b/src/entity/dataset/utils.ts @@ -21,6 +21,28 @@ export const createLocationFromMapbox = async ( }); }; +/** + * Find an existing location by externalId or create one from Mapbox. + */ +export const findOrCreateDatasetLocation = async ( + con: DataSource, + externalLocationId: string | null | undefined, +): Promise => { + if (!externalLocationId) { + return null; + } + + let location = await con.getRepository(DatasetLocation).findOne({ + where: { externalId: externalLocationId }, + }); + + if (!location) { + location = await createLocationFromMapbox(con, externalLocationId); + } + + return location; +}; + /** * Find an existing location in the dataset_location table based on iso2 country code. */ diff --git a/src/schema/opportunity.ts b/src/schema/opportunity.ts index 9ab5e9a814..c2430e0242 100644 --- a/src/schema/opportunity.ts +++ b/src/schema/opportunity.ts @@ -98,7 +98,7 @@ import { } from '../common/schema/organizations'; import { DatasetLocation } from '../entity/dataset/DatasetLocation'; import { - createLocationFromMapbox, + findOrCreateDatasetLocation, findDatasetLocation, } from '../entity/dataset/utils'; import { OpportunityLocation } from '../entity/opportunities/OpportunityLocation'; @@ -1127,12 +1127,10 @@ async function handleOpportunityLocationUpdate( opportunityId, }); - let location = await entityManager.getRepository(DatasetLocation).findOne({ - where: { externalId: externalLocationId }, - }); - if (!location) { - location = await createLocationFromMapbox(ctx.con, externalLocationId); - } + const location = await findOrCreateDatasetLocation( + ctx.con, + externalLocationId, + ); // Create new OpportunityLocation relationship if (location) { @@ -1185,17 +1183,10 @@ async function handleOpportunityOrganizationUpdate( delete organizationUpdate.externalLocationId; if (externalLocationId) { - let location = await entityManager - .getRepository(DatasetLocation) - .findOne({ - where: { externalId: externalLocationId }, - }); - if (!location) { - location = await createLocationFromMapbox( - entityManager.connection, - externalLocationId, - ); - } + const location = await findOrCreateDatasetLocation( + entityManager.connection, + externalLocationId, + ); if (location) { organizationUpdate.locationId = location.id; @@ -1893,19 +1884,10 @@ export const resolvers: IResolvers = traceResolvers< } // Handle externalLocationId -> locationId mapping - let location: DatasetLocation | null = null; - if (preferences.data.externalLocationId) { - location = await con.getRepository(DatasetLocation).findOne({ - where: { externalId: preferences.data.externalLocationId }, - }); - - if (!location) { - location = await createLocationFromMapbox( - con, - preferences.data.externalLocationId, - ); - } - } + const location = await findOrCreateDatasetLocation( + con, + preferences.data.externalLocationId, + ); await con.getRepository(UserCandidatePreference).upsert( { diff --git a/src/schema/organizations.ts b/src/schema/organizations.ts index 812e1ed6e9..793c6ed8b8 100644 --- a/src/schema/organizations.ts +++ b/src/schema/organizations.ts @@ -5,8 +5,7 @@ import { ForbiddenError } from 'apollo-server-errors'; import type { AuthContext, BaseContext, Context } from '../Context'; import { traceResolvers } from './trace'; import { Organization } from '../entity/Organization'; -import { DatasetLocation } from '../entity/dataset/DatasetLocation'; -import { createLocationFromMapbox } from '../entity/dataset/utils'; +import { findOrCreateDatasetLocation } from '../entity/dataset/utils'; import { isRoleAtLeast, OrganizationMemberRole, @@ -750,15 +749,10 @@ export const resolvers: IResolvers = traceResolvers< // Handle location update if (externalLocationId) { - let location = await ctx.con.getRepository(DatasetLocation).findOne({ - where: { externalId: externalLocationId }, - }); - if (!location) { - location = await createLocationFromMapbox( - ctx.con, - externalLocationId, - ); - } + const location = await findOrCreateDatasetLocation( + ctx.con, + externalLocationId, + ); if (location) { updatePayload.locationId = location.id; diff --git a/src/schema/profile.ts b/src/schema/profile.ts index a3c62ecae4..ff0181b6ce 100644 --- a/src/schema/profile.ts +++ b/src/schema/profile.ts @@ -15,7 +15,6 @@ import type { Connection } from 'graphql-relay'; import { UserExperience } from '../entity/user/experiences/UserExperience'; import { Company } from '../entity/Company'; import type { GraphQLResolveInfo } from 'graphql'; -import { DatasetLocation } from '../entity/dataset/DatasetLocation'; import { UserExperienceWork } from '../entity/user/experiences/UserExperienceWork'; import { AutocompleteType, @@ -26,7 +25,7 @@ import { getNonExistingSkills, insertOrIgnoreUserExperienceSkills, } from '../entity/user/experiences/UserExperienceSkill'; -import { createLocationFromMapbox } from '../entity/dataset/utils'; +import { findOrCreateDatasetLocation } from '../entity/dataset/utils'; import { User } from '../entity/user/User'; interface GQLUserExperience { id: string; @@ -326,18 +325,10 @@ export const resolvers = traceResolvers({ ): Promise => { const result = await generateExperienceToSave(ctx, args); - let location: DatasetLocation | null = null; - if (result.parsedInput.externalLocationId) { - location = await ctx.con.getRepository(DatasetLocation).findOne({ - where: { externalId: result.parsedInput.externalLocationId }, - }); - if (!location) { - location = await createLocationFromMapbox( - ctx.con, - result.parsedInput.externalLocationId, - ); - } - } + const location = await findOrCreateDatasetLocation( + ctx.con, + result.parsedInput.externalLocationId, + ); const entity = await ctx.con.transaction(async (con) => { const repo = con.getRepository(UserExperienceWork); diff --git a/src/schema/users.ts b/src/schema/users.ts index e7ba26a96a..42e0141f9a 100644 --- a/src/schema/users.ts +++ b/src/schema/users.ts @@ -167,8 +167,7 @@ import { fileTypeFromBuffer } from 'file-type'; import { notificationFlagsSchema } from '../common/schema/notificationFlagsSchema'; import { syncNotificationFlagsToCio } from '../cio'; import { UserCandidatePreference } from '../entity/user/UserCandidatePreference'; -import { DatasetLocation } from '../entity/dataset/DatasetLocation'; -import { createLocationFromMapbox } from '../entity/dataset/utils'; +import { findOrCreateDatasetLocation } from '../entity/dataset/utils'; export interface GQLUpdateUserInput { name: string; @@ -2510,20 +2509,10 @@ export const resolvers: IResolvers = traceResolvers< delete data.infoConfirmed; } data = await validateUserUpdate(user, data, ctx.con); - let location: DatasetLocation | null = null; - - if (data.externalLocationId) { - location = await ctx.con.getRepository(DatasetLocation).findOne({ - where: { externalId: data.externalLocationId }, - }); - - if (!location) { - location = await createLocationFromMapbox( - ctx.con, - data.externalLocationId, - ); - } - } + const location = await findOrCreateDatasetLocation( + ctx.con, + data.externalLocationId, + ); const filesToClear = [];