Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. import { Model, RailsFormat } from "./model";
  2. import { Address } from "./address";
  3. import { Phone } from "./phone";
  4.  
  5. export class Contact extends Model implements RailsFormat {
  6.  
  7. constructor(
  8. private numberOfAddresses = 0,
  9. private numberOfPhones = 0,
  10.  
  11. public id: number = 0,
  12. public firstName: string = "",
  13. public lastName: string = "",
  14. public email: string = "",
  15. public gender: number = 0,
  16. public contactType: number = 0,
  17. public school: string = "",
  18. public grade: number = 7,
  19. public joiningGrade: number = 0,
  20. public salutation: string = "",
  21. public dob: Date = null,
  22. public medicalCondition: string = "",
  23. public promoCode: string = "",
  24. public marketingSources: string = "",
  25. public newsletterSubscription: boolean = false,
  26. public instalments: number = 0,
  27. public employmentStart: Date = null,
  28. public updatedAt: Date = null,
  29. public createdAt: Date = null,
  30.  
  31. public addresses: Address[] = [],
  32. public phones: Phone[] = [],
  33. ) {
  34. super();
  35.  
  36. for (let i = 0; i < numberOfAddresses; i++) {
  37. addresses.push(new Address());
  38. }
  39.  
  40. for (let i = 0; i < numberOfPhones; i++) {
  41. phones.push(new Phone());
  42. }
  43. }
  44.  
  45.  
  46. static clone(obj: any) {
  47. const contact: Contact = new this();
  48.  
  49. if (obj.id) { contact.id = obj.id }
  50. if (obj.firstName) { contact.firstName = obj.firstName }
  51. if (obj.lastName) { contact.lastName = obj.lastName }
  52. if (obj.email) { contact.email = obj.email }
  53. if (obj.gender) { contact.gender = obj.gender }
  54. if (obj.contactType) { contact.contactType = obj.contactType }
  55. if (obj.school) { contact.school = obj.school }
  56. if (obj.grade) { contact.grade = obj.grade }
  57. if (obj.joiningGrade) { contact.joiningGrade = obj.joiningGrade }
  58. if (obj.salutation) { contact.salutation = obj.salutation }
  59. if (obj.dob) { contact.dob = new Date(obj.dob) }
  60. if (obj.medicalCondition) { contact.medicalCondition = obj.medicalCondition }
  61. if (obj.promoCode) { contact.promoCode = obj.promoCode }
  62. if (obj.marketingSources) { contact.marketingSources = obj.marketingSources }
  63. if (obj.newsletterSubscription) { contact.newsletterSubscription = obj.newsletterSubscription }
  64. if (obj.instalments) { contact.instalments = obj.instalments }
  65. if (obj.employmentStart) { contact.employmentStart = obj.employmentStart }
  66. if (obj.updatedAt) { contact.updatedAt = obj.updatedAt }
  67. if (obj.createdAt) { contact.createdAt = obj.createdAt }
  68.  
  69. if (obj.addresses && obj.addresses.length > 0) {
  70. obj.addresses.forEach((a: any) => {
  71. contact.addresses.push(Address.clone(a));
  72. });
  73. }
  74.  
  75. if (obj.phones && obj.phones.length > 0) {
  76. obj.phones.forEach((p: any) => {
  77. contact.phones.push(Phone.clone(p));
  78. });
  79. }
  80.  
  81. if (obj.errors && Object.keys(obj.errors).length > 0) {
  82. contact.errors = Model.getJsonApiErrors(obj.errors);
  83. }
  84.  
  85. return contact;
  86. }
  87.  
  88. serializeForRails(method: string, model: any): any {
  89. const response = Object.assign({}, model);
  90.  
  91. response.addressesAttributes = response.addresses;
  92. response.phonesAttributes = response.phones;
  93.  
  94. return response;
  95. }
  96.  
  97.  
  98. fullName() {
  99. return `${this.firstName} ${this.lastName}`;
  100. }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement