@@ -73,6 +73,7 @@ import * as gondulModule from '../../src/common/gondul';
7373import type { ServiceClient } from '../../src/types' ;
7474import { OpportunityJob } from '../../src/entity/opportunities/OpportunityJob' ;
7575import * as brokkrCommon from '../../src/common/brokkr' ;
76+ import { randomUUID } from 'node:crypto' ;
7677
7778const deleteFileFromBucket = jest . spyOn ( googleCloud , 'deleteFileFromBucket' ) ;
7879const uploadEmploymentAgreementFromBuffer = jest . spyOn (
@@ -4319,6 +4320,216 @@ describe('mutation editOpportunity', () => {
43194320 expect ( userAfter ?. title ) . toBe ( 'Updated Title Only' ) ;
43204321 expect ( userAfter ?. bio ) . toBe ( 'Initial bio that should remain' ) ;
43214322 } ) ;
4323+
4324+ it ( 'should create organization for opportunity if missing' , async ( ) => {
4325+ loggedUser = '1' ;
4326+
4327+ const MUTATION_WITH_ORG = /* GraphQL */ `
4328+ mutation EditOpportunityWithOrg(
4329+ $id: ID!
4330+ $payload: OpportunityEditInput!
4331+ ) {
4332+ editOpportunity(id: $id, payload: $payload) {
4333+ id
4334+ organization {
4335+ id
4336+ name
4337+ website
4338+ description
4339+ perks
4340+ founded
4341+ location
4342+ category
4343+ size
4344+ stage
4345+ }
4346+ }
4347+ }
4348+ ` ;
4349+
4350+ const opportunityWithoutOrganization = await con
4351+ . getRepository ( OpportunityJob )
4352+ . save ( {
4353+ ...opportunitiesFixture [ 0 ] ,
4354+ id : randomUUID ( ) ,
4355+ state : OpportunityState . DRAFT ,
4356+ organizationId : null ,
4357+ } ) ;
4358+
4359+ await con . getRepository ( OpportunityUser ) . save ( {
4360+ opportunityId : opportunityWithoutOrganization . id ,
4361+ userId : loggedUser ,
4362+ type : OpportunityUserType . Recruiter ,
4363+ } ) ;
4364+
4365+ const organizationBefore = await con . getRepository ( Organization ) . findOne ( {
4366+ where : {
4367+ name : 'Test Corp' ,
4368+ } ,
4369+ } ) ;
4370+
4371+ expect ( organizationBefore ) . toBeNull ( ) ;
4372+
4373+ const res = await client . mutate ( MUTATION_WITH_ORG , {
4374+ variables : {
4375+ id : opportunityWithoutOrganization . id ,
4376+ payload : {
4377+ organization : {
4378+ name : 'Test Corp' ,
4379+ website : 'https://updated.dev' ,
4380+ description : 'Updated description' ,
4381+ perks : [ 'Remote work' , 'Flexible hours' ] ,
4382+ founded : 2021 ,
4383+ location : 'Berlin, Germany' ,
4384+ category : 'Technology' ,
4385+ size : CompanySize . COMPANY_SIZE_51_200 ,
4386+ stage : CompanyStage . SERIES_B ,
4387+ } ,
4388+ } ,
4389+ } ,
4390+ } ) ;
4391+
4392+ expect ( res . errors ) . toBeFalsy ( ) ;
4393+ expect ( res . data . editOpportunity . organization ) . toMatchObject ( {
4394+ name : 'Test Corp' ,
4395+ website : 'https://updated.dev' ,
4396+ description : 'Updated description' ,
4397+ perks : [ 'Remote work' , 'Flexible hours' ] ,
4398+ founded : 2021 ,
4399+ location : 'Berlin, Germany' ,
4400+ category : 'Technology' ,
4401+ size : CompanySize . COMPANY_SIZE_51_200 ,
4402+ stage : CompanyStage . SERIES_B ,
4403+ } ) ;
4404+
4405+ // Verify the organization was created in database
4406+ const organization = await con
4407+ . getRepository ( Organization )
4408+ . findOneBy ( { id : res . data . editOpportunity . organization . id } ) ;
4409+
4410+ expect ( organization ) . toMatchObject ( {
4411+ name : 'Test Corp' ,
4412+ website : 'https://updated.dev' ,
4413+ description : 'Updated description' ,
4414+ perks : [ 'Remote work' , 'Flexible hours' ] ,
4415+ founded : 2021 ,
4416+ location : 'Berlin, Germany' ,
4417+ category : 'Technology' ,
4418+ size : CompanySize . COMPANY_SIZE_51_200 ,
4419+ stage : CompanyStage . SERIES_B ,
4420+ } ) ;
4421+
4422+ const opportunityAfter = await con
4423+ . getRepository ( OpportunityJob )
4424+ . findOneBy ( { id : opportunityWithoutOrganization . id } ) ;
4425+
4426+ expect ( opportunityAfter ! . organizationId ) . toBe (
4427+ res . data . editOpportunity . organization . id ,
4428+ ) ;
4429+ } ) ;
4430+
4431+ it ( 'should not update organization name on edit' , async ( ) => {
4432+ loggedUser = '1' ;
4433+
4434+ const MUTATION_WITH_ORG = /* GraphQL */ `
4435+ mutation EditOpportunityWithOrg(
4436+ $id: ID!
4437+ $payload: OpportunityEditInput!
4438+ ) {
4439+ editOpportunity(id: $id, payload: $payload) {
4440+ id
4441+ organization {
4442+ id
4443+ name
4444+ }
4445+ }
4446+ }
4447+ ` ;
4448+
4449+ const res = await client . mutate ( MUTATION_WITH_ORG , {
4450+ variables : {
4451+ id : opportunitiesFixture [ 0 ] . id ,
4452+ payload : {
4453+ organization : {
4454+ name : 'Test update name' ,
4455+ } ,
4456+ } ,
4457+ } ,
4458+ } ) ;
4459+
4460+ expect ( res . errors ) . toBeFalsy ( ) ;
4461+ expect ( res . data . editOpportunity . organization . name ) . toEqual (
4462+ organizationsFixture [ 0 ] . name ,
4463+ ) ;
4464+
4465+ // Verify the organization was updated in database
4466+ const organization = await con
4467+ . getRepository ( Organization )
4468+ . findOneBy ( { id : organizationsFixture [ 0 ] . id } ) ;
4469+
4470+ expect ( organization ! . name ) . toEqual ( organizationsFixture [ 0 ] . name ) ;
4471+ } ) ;
4472+
4473+ it ( 'should not allow duplicate organization names' , async ( ) => {
4474+ loggedUser = '1' ;
4475+
4476+ const MUTATION_WITH_ORG = /* GraphQL */ `
4477+ mutation EditOpportunityWithOrg(
4478+ $id: ID!
4479+ $payload: OpportunityEditInput!
4480+ ) {
4481+ editOpportunity(id: $id, payload: $payload) {
4482+ id
4483+ organization {
4484+ id
4485+ name
4486+ }
4487+ }
4488+ }
4489+ ` ;
4490+
4491+ const opportunityWithoutOrganization = await con
4492+ . getRepository ( OpportunityJob )
4493+ . save ( {
4494+ ...opportunitiesFixture [ 0 ] ,
4495+ id : randomUUID ( ) ,
4496+ state : OpportunityState . DRAFT ,
4497+ organizationId : null ,
4498+ } ) ;
4499+
4500+ await con . getRepository ( OpportunityUser ) . save ( {
4501+ opportunityId : opportunityWithoutOrganization . id ,
4502+ userId : loggedUser ,
4503+ type : OpportunityUserType . Recruiter ,
4504+ } ) ;
4505+
4506+ const organizationBefore = await con . getRepository ( Organization ) . findOne ( {
4507+ where : {
4508+ name : 'Daily Dev Inc' ,
4509+ } ,
4510+ } ) ;
4511+
4512+ expect ( organizationBefore ) . not . toBeNull ( ) ;
4513+
4514+ const res = await client . mutate ( MUTATION_WITH_ORG , {
4515+ variables : {
4516+ id : opportunityWithoutOrganization . id ,
4517+ payload : {
4518+ organization : {
4519+ name : 'Daily Dev Inc' ,
4520+ founded : 2021 ,
4521+ } ,
4522+ } ,
4523+ } ,
4524+ } ) ;
4525+
4526+ expect ( res . errors ) . toBeTruthy ( ) ;
4527+
4528+ expect ( res . errors ! [ 0 ] . extensions . code ) . toEqual ( 'CONFLICT' ) ;
4529+ expect ( res . errors ! [ 0 ] . message ) . toEqual (
4530+ 'Organization with this name already exists' ,
4531+ ) ;
4532+ } ) ;
43224533} ) ;
43234534
43244535describe ( 'mutation clearOrganizationImage' , ( ) => {
0 commit comments