diff --git a/client/app/components/projects/new.js b/client/app/components/projects/new.js index 20ad8420..8b869d76 100644 --- a/client/app/components/projects/new.js +++ b/client/app/components/projects/new.js @@ -3,7 +3,7 @@ import { action } from '@ember/object'; import { inject as service } from '@ember/service'; import SubmittableProjectsNewForm from '../../validations/submittable-projects-new-form'; import { optionset } from '../../helpers/optionset'; - +import { DCPAPPLICANTROLE } from '../../optionsets/project-applicant'; export default class ProjectsNewFormComponent extends Component { validations = { @@ -35,14 +35,12 @@ export default class ProjectsNewFormComponent extends Component { applicantType: this.args.package.applicantType.code, }; - console.log("projectInformation", projectInformation); - const primaryContactInput = { first: this.args.package.primaryContactFirstName, last: this.args.package.primaryContactLastName, email: this.args.package.primaryContactEmail, phone: this.args.package.primaryContactPhone, - role: 'contact', + role: DCPAPPLICANTROLE.PRIMARY_CONTACT.code, }; const applicantInput = { @@ -50,29 +48,35 @@ export default class ProjectsNewFormComponent extends Component { last: this.args.package.applicantLastName, email: this.args.package.applicantEmail, phone: this.args.package.applicantPhone, - role: 'applicant', + role: DCPAPPLICANTROLE.PRIMARY_APPLICANT.code, }; - let newProject = this.store.createRecord("project", { - dcpProjectname: projectInformation.projectName, - dcpBorough: projectInformation.borough, - dcpApplicanttype: projectInformation.applicantType, - }); - // return projectModel.save(); - - console.log('new project?', newProject); - console.log('new project?', newProject.changedAttributes()); - const contactInputs = [primaryContactInput, applicantInput]; try { - const contactPromises = contactInputs.map( - (contact) => this.store.queryRecord('contact', - { - email: contact.email, - includeAllStatusCodes: true, - }), - ); + // Create and save the initial project model + const projectModel = this.store.createRecord('project', { + dcpProjectname: projectInformation.projectName, + dcpBorough: projectInformation.borough, + dcpApplicanttype: projectInformation.applicantType, + }); + + await projectModel.save(); + console.log('new project?', projectModel); + + // Fetch and resolve the most recent project + const projects = await this.store.findAll('project'); + const sortedProjects = projects.sortBy('createdAt').reverse(); + const mostRecentProject = sortedProjects.get('firstObject'); + + console.log('Resolved most recent project:', mostRecentProject); + + // Proceed with contact promises + const contactPromises = contactInputs.map((contact) => this.store.queryRecord('contact', + { + email: contact.email, + includeAllStatusCodes: true, + })); const contacts = await Promise.all(contactPromises); @@ -90,21 +94,43 @@ export default class ProjectsNewFormComponent extends Component { return contact; }); - // const saveProjectInformation = () => { - // const projectModel = this.store.createRecord('project', { - // dcpProjectName: projectInformation.projectName, - // dcpBorough: projectInformation.borough, - // dcoApplicantType: projectInformation.applicantType, - // }); - // return projectModel.save(); - // }; - - await Promise.all(verifiedContactPromises) - .then( - await this.args.package.submit(), + const verifiedContacts = await Promise.all(verifiedContactPromises); + + const applicantRoleCodes = [DCPAPPLICANTROLE.PRIMARY_CONTACT.code, DCPAPPLICANTROLE.PRIMARY_APPLICANT.code]; + const applicants = applicantRoleCodes.map((code) => { + const applicant = this.store.createRecord('project-applicant', { + dcpApplicantrole: code, + }); + const input = contactInputs.find((input) => input.role === code); + if (input === undefined) throw new Error('Applicant role not found'); + const { email } = input; + const contact = verifiedContacts.find((contact) => contact.emailaddress1 === email); + if (contact === undefined) throw new Error('Contact for applicant role not found'); + applicant.contact = contact; + return applicant; + }); + + console.log('applicants', applicants); + + applicants.forEach( + (applicant) => mostRecentProject.projectApplicants.pushObject(applicant), + ); + applicants.forEach((applicant) => { + console.log('applicant', applicant); + console.log( + 'mostRecentProject.projectApplicants', + mostRecentProject.projectApplicants, ); + applicant.mostRecentProject = mostRecentProject; + applicant.save(); + }); + + await mostRecentProject.save(); + + // .then(await this.args.package.submit()); } catch (error) { - console.log('Save new project package error', error); + console.log('Save new project package error', error.message); + console.log('the error', error); } } } diff --git a/client/app/serializers/project.js b/client/app/serializers/project.js new file mode 100644 index 00000000..4172e1c4 --- /dev/null +++ b/client/app/serializers/project.js @@ -0,0 +1,8 @@ +import { EmbeddedRecordsMixin } from '@ember-data/serializer/rest'; +import JSONAPISerializer from "@ember-data/serializer/json-api"; + +export default JSONAPISerializer.extend(EmbeddedRecordsMixin, { + attrs: { + projectApplicants: { embedded: "always" }, + }, +});