Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/entity/dataset/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DatasetLocation | null> => {
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.
*/
Expand Down
44 changes: 13 additions & 31 deletions src/schema/opportunity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1893,19 +1884,10 @@ export const resolvers: IResolvers<unknown, BaseContext> = 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(
{
Expand Down
16 changes: 5 additions & 11 deletions src/schema/organizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -750,15 +749,10 @@ export const resolvers: IResolvers<unknown, BaseContext> = 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;
Expand Down
19 changes: 5 additions & 14 deletions src/schema/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -326,18 +325,10 @@ export const resolvers = traceResolvers<unknown, AuthContext>({
): Promise<GQLUserExperience> => {
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);
Expand Down
21 changes: 5 additions & 16 deletions src/schema/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -2510,20 +2509,10 @@ export const resolvers: IResolvers<unknown, BaseContext> = 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 = [];

Expand Down
Loading