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
69 changes: 69 additions & 0 deletions __tests__/schema/opportunity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5600,6 +5600,9 @@ describe('mutation parseOpportunity', () => {
title
placeholder
}
organization {
id
}
}
}
`;
Expand Down Expand Up @@ -6036,6 +6039,72 @@ describe('mutation parseOpportunity', () => {

expect(opportunityRecruiter).toBeDefined();
});

it('should assign opportunity to existing organization of authenticated user', async () => {
loggedUser = '1';

trackingId = 'anon1';

fileTypeFromBuffer.mockResolvedValue({
ext: 'pdf',
mime: 'application/pdf',
});

const uploadResumeFromBufferSpy = jest.spyOn(
googleCloud,
'uploadResumeFromBuffer',
);

uploadResumeFromBufferSpy.mockResolvedValue(
`https://storage.cloud.google.com/${RESUME_BUCKET_NAME}/file`,
);

const deleteFileFromBucketSpy = jest.spyOn(
googleCloud,
'deleteFileFromBucket',
);

deleteFileFromBucketSpy.mockResolvedValue(true);

console.log(await con.getRepository(OpportunityUserRecruiter).find());

// Execute the mutation with a file upload
const res = await authorizeRequest(
request(app.server)
.post('/graphql')
.field(
'operations',
JSON.stringify({
query: MUTATION,
variables: {
payload: {
file: null,
},
},
}),
)
.field('map', JSON.stringify({ '0': ['variables.payload.file'] }))
.attach('0', './__tests__/fixture/screen.pdf'),
).expect(200);

const body = res.body;
expect(body.errors).toBeFalsy();

expect(body.data.parseOpportunity).toMatchObject({
title: 'Mocked Opportunity Title',
organization: { id: '550e8400-e29b-41d4-a716-446655440000' },
});

const opportunity = await con.getRepository(OpportunityJob).findOne({
where: {
id: body.data.parseOpportunity.id,
},
});

expect(opportunity!.organizationId).toBe(
'550e8400-e29b-41d4-a716-446655440000',
);
});
});

describe('mutation createSharedSlackChannel', () => {
Expand Down
73 changes: 36 additions & 37 deletions src/common/schema/opportunities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,46 +83,45 @@ export const opportunityCreateSchema = z.object({
content: opportunityContentSchema.partial(),
});

export const opportunityCreateParseSchema = opportunityCreateSchema
.omit({ organizationId: true })
.extend({
keywords: z.preprocess((val) => {
if (Array.isArray(val)) {
return val.map((keyword) => {
return {
keyword,
};
});
}
export const opportunityCreateParseSchema = opportunityCreateSchema.extend({
organizationId: opportunityCreateSchema.shape.organizationId.nullish(),
keywords: z.preprocess((val) => {
if (Array.isArray(val)) {
return val.map((keyword) => {
return {
keyword,
};
});
}

return val;
}, opportunityCreateSchema.shape.keywords),
meta: opportunityCreateSchema.shape.meta
.extend({
teamSize: opportunityCreateSchema.shape.meta.shape.teamSize.optional(),
salary: z
.object({
min: z.preprocess((val: bigint) => {
if (typeof val === 'undefined') {
return val;
}
return val;
}, opportunityCreateSchema.shape.keywords),
meta: opportunityCreateSchema.shape.meta
.extend({
teamSize: opportunityCreateSchema.shape.meta.shape.teamSize.optional(),
salary: z
.object({
min: z.preprocess((val: bigint) => {
if (typeof val === 'undefined') {
return val;
}

return parseBigInt(val);
}, z.number().int().nonnegative().max(100_000_000).optional()),
max: z.preprocess((val: bigint) => {
if (typeof val === 'undefined') {
return val;
}
return parseBigInt(val);
}, z.number().int().nonnegative().max(100_000_000).optional()),
max: z.preprocess((val: bigint) => {
if (typeof val === 'undefined') {
return val;
}

return parseBigInt(val);
}, z.number().int().nonnegative().max(100_000_000).optional()),
period: z.number(),
})
.partial()
.optional(),
})
.partial(),
});
return parseBigInt(val);
}, z.number().int().nonnegative().max(100_000_000).optional()),
period: z.number(),
})
.partial()
.optional(),
})
.partial(),
});

export const opportunityEditSchema = z
.object({
Expand Down
24 changes: 24 additions & 0 deletions src/schema/opportunity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ import {
type DeepPartial,
JsonContains,
EntityManager,
IsNull,
} from 'typeorm';
import { Organization } from '../entity/Organization';
import { ContentPreferenceOrganization } from '../entity/contentPreference/ContentPreferenceOrganization';
Expand Down Expand Up @@ -2870,6 +2871,29 @@ export const resolvers: IResolvers<unknown, BaseContext> = traceResolvers<
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { location, ...opportunityData } = parsedOpportunity;

// if user is logged in, associate them with their existing organization
if (ctx.userId) {
const existingOrganizationOpportunity: Pick<
OpportunityJob,
'id' | 'organizationId'
> | null = await entityManager
.getRepository(OpportunityJob)
.findOne({
select: ['id', 'organizationId'],
where: {
users: {
userId: ctx.userId,
},
organizationId: Not(IsNull()),
},
});

if (existingOrganizationOpportunity) {
opportunityData.organizationId =
existingOrganizationOpportunity.organizationId;
}
}

const opportunity = await entityManager
.getRepository(OpportunityJob)
.save(
Expand Down
Loading