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
51 changes: 51 additions & 0 deletions __tests__/schema/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ describe('mutation upsertUserGeneralExperience', () => {
description: 'Built a popular browser extension for developers',
startedAt: new Date('2021-01-01'),
url: 'https://github.com/dailydotdev/extension',
customCompanyName: 'Personal Project',
},
},
});
Expand Down Expand Up @@ -741,13 +742,15 @@ describe('mutation upsertUserGeneralExperience', () => {
title: 'Self-taught Developer',
startedAt: new Date('2020-01-01'),
companyId: null,
customCompanyName: 'Self-taught',
},
},
});

expect(res.errors).toBeFalsy();
expect(res.data.upsertUserGeneralExperience).toMatchObject({
company: null,
customCompanyName: 'Self-taught',
});
});

Expand Down Expand Up @@ -784,6 +787,7 @@ describe('mutation upsertUserGeneralExperience', () => {
type: 'certification',
title: 'Test',
startedAt: new Date('2020-01-01'),
customCompanyName: 'Test Company',
},
},
},
Expand All @@ -805,6 +809,7 @@ describe('mutation upsertUserGeneralExperience', () => {
type: 'work',
title: 'Hacked Title',
startedAt: new Date('2021-01-01'),
customCompanyName: 'Some Company',
},
},
},
Expand All @@ -829,6 +834,7 @@ describe('mutation upsertUserGeneralExperience', () => {
type: 'project',
title: 'Updated Project Title',
startedAt: new Date('2021-06-01'),
companyId: 'company-1',
},
},
});
Expand Down Expand Up @@ -959,6 +965,29 @@ describe('mutation upsertUserGeneralExperience', () => {
);
});

it('should fail when experience has neither companyId nor customCompanyName', async () => {
loggedUser = '1';

const experienceTypes = ['work', 'education', 'project', 'certification'];

for (const type of experienceTypes) {
await testQueryErrorCode(
client,
{
query: UPSERT_USER_GENERAL_EXPERIENCE_MUTATION,
variables: {
input: {
type,
title: 'Test Experience',
startedAt: new Date('2023-01-01'),
},
},
},
'ZOD_VALIDATION_ERROR',
);
}
});

it('should create experience without optional fields', async () => {
loggedUser = '1';

Expand All @@ -968,6 +997,7 @@ describe('mutation upsertUserGeneralExperience', () => {
type: 'project',
title: 'Minimal Project',
startedAt: new Date('2023-01-01'),
customCompanyName: 'Project Organization',
},
},
});
Expand All @@ -981,6 +1011,7 @@ describe('mutation upsertUserGeneralExperience', () => {
endedAt: null,
url: null,
company: null,
customCompanyName: 'Project Organization',
});
});

Expand Down Expand Up @@ -1244,6 +1275,7 @@ describe('mutation upsertUserWorkExperience', () => {
type: 'work',
title: 'Hacked Title',
startedAt: new Date('2021-01-01'),
customCompanyName: 'Some Company',
skills: [],
},
},
Expand Down Expand Up @@ -1502,6 +1534,25 @@ describe('mutation upsertUserWorkExperience', () => {
skills: [],
});
});

it('should fail when work experience has neither companyId nor customCompanyName', async () => {
loggedUser = '1';

await testQueryErrorCode(
client,
{
query: UPSERT_USER_WORK_EXPERIENCE_MUTATION,
variables: {
input: {
type: 'work',
title: 'Software Engineer',
startedAt: new Date('2023-01-01'),
},
},
},
'ZOD_VALIDATION_ERROR',
);
});
});

describe('mutation removeUserExperience', () => {
Expand Down
20 changes: 17 additions & 3 deletions src/common/schema/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ export const userExperienceInputBaseSchema = z.object({
.trim()
.normalize()
.max(100)
.nonempty()
.nullable()
.optional()
.nullish()
.default(null),
});

Expand Down Expand Up @@ -72,6 +70,15 @@ const experienceTypeToSchema: Record<
[UserExperienceType.OpenSource]: userExperienceProjectSchema,
};

const experienceCompanyCopy = {
[UserExperienceType.Work]: 'Company',
[UserExperienceType.Education]: 'School',
[UserExperienceType.Project]: 'Publisher',
[UserExperienceType.Certification]: 'Organization',
[UserExperienceType.OpenSource]: 'Organization',
[UserExperienceType.Volunteering]: 'Organization',
};

export const getExperienceSchema = (type: UserExperienceType) => {
return experienceTypeToSchema[type].superRefine((data, ctx) => {
if (data.endedAt && data.endedAt < data.startedAt) {
Expand All @@ -81,6 +88,13 @@ export const getExperienceSchema = (type: UserExperienceType) => {
path: ['endedAt'],
});
}
if (!data.customCompanyName && !data.companyId) {
ctx.addIssue({
code: 'custom',
message: `${experienceCompanyCopy[type]} is required`,
path: ['customCompanyName'],
});
}
});
};

Expand Down
Loading