Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Model, RailsFormat } from "./model";
- import { Address } from "./address";
- import { Phone } from "./phone";
- export class Contact extends Model implements RailsFormat {
- constructor(
- private numberOfAddresses = 0,
- private numberOfPhones = 0,
- public id: number = 0,
- public firstName: string = "",
- public lastName: string = "",
- public email: string = "",
- public gender: number = 0,
- public contactType: number = 0,
- public school: string = "",
- public grade: number = 7,
- public joiningGrade: number = 0,
- public salutation: string = "",
- public dob: Date = null,
- public medicalCondition: string = "",
- public promoCode: string = "",
- public marketingSources: string = "",
- public newsletterSubscription: boolean = false,
- public instalments: number = 0,
- public employmentStart: Date = null,
- public updatedAt: Date = null,
- public createdAt: Date = null,
- public addresses: Address[] = [],
- public phones: Phone[] = [],
- ) {
- super();
- for (let i = 0; i < numberOfAddresses; i++) {
- addresses.push(new Address());
- }
- for (let i = 0; i < numberOfPhones; i++) {
- phones.push(new Phone());
- }
- }
- static clone(obj: any) {
- const contact: Contact = new this();
- if (obj.id) { contact.id = obj.id }
- if (obj.firstName) { contact.firstName = obj.firstName }
- if (obj.lastName) { contact.lastName = obj.lastName }
- if (obj.email) { contact.email = obj.email }
- if (obj.gender) { contact.gender = obj.gender }
- if (obj.contactType) { contact.contactType = obj.contactType }
- if (obj.school) { contact.school = obj.school }
- if (obj.grade) { contact.grade = obj.grade }
- if (obj.joiningGrade) { contact.joiningGrade = obj.joiningGrade }
- if (obj.salutation) { contact.salutation = obj.salutation }
- if (obj.dob) { contact.dob = new Date(obj.dob) }
- if (obj.medicalCondition) { contact.medicalCondition = obj.medicalCondition }
- if (obj.promoCode) { contact.promoCode = obj.promoCode }
- if (obj.marketingSources) { contact.marketingSources = obj.marketingSources }
- if (obj.newsletterSubscription) { contact.newsletterSubscription = obj.newsletterSubscription }
- if (obj.instalments) { contact.instalments = obj.instalments }
- if (obj.employmentStart) { contact.employmentStart = obj.employmentStart }
- if (obj.updatedAt) { contact.updatedAt = obj.updatedAt }
- if (obj.createdAt) { contact.createdAt = obj.createdAt }
- if (obj.addresses && obj.addresses.length > 0) {
- obj.addresses.forEach((a: any) => {
- contact.addresses.push(Address.clone(a));
- });
- }
- if (obj.phones && obj.phones.length > 0) {
- obj.phones.forEach((p: any) => {
- contact.phones.push(Phone.clone(p));
- });
- }
- if (obj.errors && Object.keys(obj.errors).length > 0) {
- contact.errors = Model.getJsonApiErrors(obj.errors);
- }
- return contact;
- }
- serializeForRails(method: string, model: any): any {
- const response = Object.assign({}, model);
- response.addressesAttributes = response.addresses;
- response.phonesAttributes = response.phones;
- return response;
- }
- fullName() {
- return `${this.firstName} ${this.lastName}`;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement