Skip to content

Commit 2486cae

Browse files
authored
feat: opportunity parse assign existing organization (#3371)
1 parent cc06fc0 commit 2486cae

3 files changed

Lines changed: 129 additions & 37 deletions

File tree

__tests__/schema/opportunity.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5600,6 +5600,9 @@ describe('mutation parseOpportunity', () => {
56005600
title
56015601
placeholder
56025602
}
5603+
organization {
5604+
id
5605+
}
56035606
}
56045607
}
56055608
`;
@@ -6036,6 +6039,72 @@ describe('mutation parseOpportunity', () => {
60366039

60376040
expect(opportunityRecruiter).toBeDefined();
60386041
});
6042+
6043+
it('should assign opportunity to existing organization of authenticated user', async () => {
6044+
loggedUser = '1';
6045+
6046+
trackingId = 'anon1';
6047+
6048+
fileTypeFromBuffer.mockResolvedValue({
6049+
ext: 'pdf',
6050+
mime: 'application/pdf',
6051+
});
6052+
6053+
const uploadResumeFromBufferSpy = jest.spyOn(
6054+
googleCloud,
6055+
'uploadResumeFromBuffer',
6056+
);
6057+
6058+
uploadResumeFromBufferSpy.mockResolvedValue(
6059+
`https://storage.cloud.google.com/${RESUME_BUCKET_NAME}/file`,
6060+
);
6061+
6062+
const deleteFileFromBucketSpy = jest.spyOn(
6063+
googleCloud,
6064+
'deleteFileFromBucket',
6065+
);
6066+
6067+
deleteFileFromBucketSpy.mockResolvedValue(true);
6068+
6069+
console.log(await con.getRepository(OpportunityUserRecruiter).find());
6070+
6071+
// Execute the mutation with a file upload
6072+
const res = await authorizeRequest(
6073+
request(app.server)
6074+
.post('/graphql')
6075+
.field(
6076+
'operations',
6077+
JSON.stringify({
6078+
query: MUTATION,
6079+
variables: {
6080+
payload: {
6081+
file: null,
6082+
},
6083+
},
6084+
}),
6085+
)
6086+
.field('map', JSON.stringify({ '0': ['variables.payload.file'] }))
6087+
.attach('0', './__tests__/fixture/screen.pdf'),
6088+
).expect(200);
6089+
6090+
const body = res.body;
6091+
expect(body.errors).toBeFalsy();
6092+
6093+
expect(body.data.parseOpportunity).toMatchObject({
6094+
title: 'Mocked Opportunity Title',
6095+
organization: { id: '550e8400-e29b-41d4-a716-446655440000' },
6096+
});
6097+
6098+
const opportunity = await con.getRepository(OpportunityJob).findOne({
6099+
where: {
6100+
id: body.data.parseOpportunity.id,
6101+
},
6102+
});
6103+
6104+
expect(opportunity!.organizationId).toBe(
6105+
'550e8400-e29b-41d4-a716-446655440000',
6106+
);
6107+
});
60396108
});
60406109

60416110
describe('mutation createSharedSlackChannel', () => {

src/common/schema/opportunities.ts

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -83,46 +83,45 @@ export const opportunityCreateSchema = z.object({
8383
content: opportunityContentSchema.partial(),
8484
});
8585

86-
export const opportunityCreateParseSchema = opportunityCreateSchema
87-
.omit({ organizationId: true })
88-
.extend({
89-
keywords: z.preprocess((val) => {
90-
if (Array.isArray(val)) {
91-
return val.map((keyword) => {
92-
return {
93-
keyword,
94-
};
95-
});
96-
}
86+
export const opportunityCreateParseSchema = opportunityCreateSchema.extend({
87+
organizationId: opportunityCreateSchema.shape.organizationId.nullish(),
88+
keywords: z.preprocess((val) => {
89+
if (Array.isArray(val)) {
90+
return val.map((keyword) => {
91+
return {
92+
keyword,
93+
};
94+
});
95+
}
9796

98-
return val;
99-
}, opportunityCreateSchema.shape.keywords),
100-
meta: opportunityCreateSchema.shape.meta
101-
.extend({
102-
teamSize: opportunityCreateSchema.shape.meta.shape.teamSize.optional(),
103-
salary: z
104-
.object({
105-
min: z.preprocess((val: bigint) => {
106-
if (typeof val === 'undefined') {
107-
return val;
108-
}
97+
return val;
98+
}, opportunityCreateSchema.shape.keywords),
99+
meta: opportunityCreateSchema.shape.meta
100+
.extend({
101+
teamSize: opportunityCreateSchema.shape.meta.shape.teamSize.optional(),
102+
salary: z
103+
.object({
104+
min: z.preprocess((val: bigint) => {
105+
if (typeof val === 'undefined') {
106+
return val;
107+
}
109108

110-
return parseBigInt(val);
111-
}, z.number().int().nonnegative().max(100_000_000).optional()),
112-
max: z.preprocess((val: bigint) => {
113-
if (typeof val === 'undefined') {
114-
return val;
115-
}
109+
return parseBigInt(val);
110+
}, z.number().int().nonnegative().max(100_000_000).optional()),
111+
max: z.preprocess((val: bigint) => {
112+
if (typeof val === 'undefined') {
113+
return val;
114+
}
116115

117-
return parseBigInt(val);
118-
}, z.number().int().nonnegative().max(100_000_000).optional()),
119-
period: z.number(),
120-
})
121-
.partial()
122-
.optional(),
123-
})
124-
.partial(),
125-
});
116+
return parseBigInt(val);
117+
}, z.number().int().nonnegative().max(100_000_000).optional()),
118+
period: z.number(),
119+
})
120+
.partial()
121+
.optional(),
122+
})
123+
.partial(),
124+
});
126125

127126
export const opportunityEditSchema = z
128127
.object({

src/schema/opportunity.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ import {
8888
type DeepPartial,
8989
JsonContains,
9090
EntityManager,
91+
IsNull,
9192
} from 'typeorm';
9293
import { Organization } from '../entity/Organization';
9394
import { ContentPreferenceOrganization } from '../entity/contentPreference/ContentPreferenceOrganization';
@@ -2870,6 +2871,29 @@ export const resolvers: IResolvers<unknown, BaseContext> = traceResolvers<
28702871
// eslint-disable-next-line @typescript-eslint/no-unused-vars
28712872
const { location, ...opportunityData } = parsedOpportunity;
28722873

2874+
// if user is logged in, associate them with their existing organization
2875+
if (ctx.userId) {
2876+
const existingOrganizationOpportunity: Pick<
2877+
OpportunityJob,
2878+
'id' | 'organizationId'
2879+
> | null = await entityManager
2880+
.getRepository(OpportunityJob)
2881+
.findOne({
2882+
select: ['id', 'organizationId'],
2883+
where: {
2884+
users: {
2885+
userId: ctx.userId,
2886+
},
2887+
organizationId: Not(IsNull()),
2888+
},
2889+
});
2890+
2891+
if (existingOrganizationOpportunity) {
2892+
opportunityData.organizationId =
2893+
existingOrganizationOpportunity.organizationId;
2894+
}
2895+
}
2896+
28732897
const opportunity = await entityManager
28742898
.getRepository(OpportunityJob)
28752899
.save(

0 commit comments

Comments
 (0)