Skip to content

Commit

Permalink
serializer and applicant roles
Browse files Browse the repository at this point in the history
  • Loading branch information
horatiorosa committed Nov 6, 2024
1 parent 84dcdd8 commit ea6ead0
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 35 deletions.
96 changes: 61 additions & 35 deletions client/app/components/projects/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -35,44 +35,48 @@ 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 = {
first: this.args.package.applicantFirstName,
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);

Expand All @@ -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);
}
}
}
8 changes: 8 additions & 0 deletions client/app/serializers/project.js
Original file line number Diff line number Diff line change
@@ -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" },
},
});

0 comments on commit ea6ead0

Please sign in to comment.