|
1 | | -import type { Address } from '@/api/dataclasses/address' |
2 | | -import type { CustomerProduct } from '@/api/dataclasses/customer_product' |
| 1 | +import { formatString } from '@/api/util' |
3 | 2 |
|
4 | | -export type CustomerProductExtension = { products: CustomerProduct[] } |
| 3 | +export interface CustomerCreate { |
| 4 | + name: string, |
| 5 | + email: string, |
| 6 | + phoneNumber?: string, |
| 7 | + /** Optional Website URL */ |
| 8 | + websiteURL?: string, |
| 9 | + /** The street of the address */ |
| 10 | + address: string, |
| 11 | + /** The house number of the address */ |
| 12 | + houseNumber: string, |
| 13 | + /** The addition to the address of the person taking care of receiving mails */ |
| 14 | + careOf?: string, |
| 15 | + /** The postal code of the address */ |
| 16 | + postalCode: string, |
| 17 | + /** The city of the address */ |
| 18 | + city: string, |
| 19 | + /** The country of the address */ |
| 20 | + country: string, |
| 21 | +} |
5 | 22 |
|
6 | | -export type Customer = { |
| 23 | +export interface Customer { |
7 | 24 | uuid: string, |
8 | 25 | name: string, |
9 | | - creationDate: Date, |
10 | | - address: Address, |
11 | 26 | email: string, |
12 | | - phoneNumber: string, |
| 27 | + phoneNumber?: string, |
13 | 28 | /** Optional Website URL */ |
14 | 29 | websiteURL?: string, |
15 | | - /** Extension that might or might not be loaded */ |
16 | | - productsExtension?: CustomerProductExtension, |
| 30 | + /** The street of the address */ |
| 31 | + address: string, |
| 32 | + /** The house number of the address */ |
| 33 | + houseNumber: string, |
| 34 | + /** The addition to the address of the person taking care of receiving mails */ |
| 35 | + careOf?: string, |
| 36 | + /** The postal code of the address */ |
| 37 | + postalCode: string, |
| 38 | + /** The city of the address */ |
| 39 | + city: string, |
| 40 | + /** The country of the address */ |
| 41 | + country: string, |
| 42 | + createdAt: Date, |
| 43 | + updatedAt: Date, |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Converts a JSON object to a Customer object. |
| 48 | + */ |
| 49 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 50 | +const fromJson = (json: any): Customer => { |
| 51 | + return { |
| 52 | + uuid: json.uuid, |
| 53 | + name: json.name, |
| 54 | + email: json.email, |
| 55 | + phoneNumber: json.phone_number, |
| 56 | + websiteURL: json.website_url, |
| 57 | + address: json.address, |
| 58 | + houseNumber: json.house_number, |
| 59 | + careOf: json.care_of, |
| 60 | + postalCode: json.postal_code, |
| 61 | + city: json.city, |
| 62 | + country: json.country, |
| 63 | + createdAt: new Date(json.created_at), |
| 64 | + updatedAt: new Date(json.updated_at), |
| 65 | + } |
17 | 66 | } |
| 67 | + |
| 68 | +/** |
| 69 | + * Converts a Customer object back to JSON. |
| 70 | + */ |
| 71 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 72 | +const toJson = (customer: Customer): Record<string, any> => { |
| 73 | + return { |
| 74 | + uuid: customer.uuid, |
| 75 | + name: customer.name, |
| 76 | + email: customer.email, |
| 77 | + phone_number: customer.phoneNumber, |
| 78 | + website_url: customer.websiteURL, |
| 79 | + address: customer.address, |
| 80 | + house_number: customer.houseNumber, |
| 81 | + care_of: customer.careOf, |
| 82 | + postal_code: customer.postalCode, |
| 83 | + city: customer.city, |
| 84 | + country: customer.country, |
| 85 | + created_at: customer.createdAt.toISOString(), |
| 86 | + updated_at: customer.updatedAt.toISOString(), |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 91 | +const toJsonUpdate = (customer: Customer): Record<string, any> => { |
| 92 | + return { |
| 93 | + name: customer.name, |
| 94 | + email: customer.email, |
| 95 | + phone_number: formatString(customer.phoneNumber), |
| 96 | + website_url: formatString(customer.websiteURL), |
| 97 | + address: formatString(customer.address), |
| 98 | + house_number: formatString(customer.houseNumber), |
| 99 | + care_of: formatString(customer.careOf), |
| 100 | + postal_code: formatString(customer.postalCode), |
| 101 | + city: formatString(customer.city), |
| 102 | + country: formatString(customer.country), |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 107 | +const toJsonCreate = (customer: CustomerCreate): Record<string, any> => { |
| 108 | + return { |
| 109 | + name: customer.name, |
| 110 | + email: customer.email, |
| 111 | + phone_number: formatString(customer.phoneNumber), |
| 112 | + website_url: formatString(customer.websiteURL), |
| 113 | + address: formatString(customer.address), |
| 114 | + house_number: formatString(customer.houseNumber), |
| 115 | + care_of: formatString(customer.careOf), |
| 116 | + postal_code: formatString(customer.postalCode), |
| 117 | + city: formatString(customer.city), |
| 118 | + country: formatString(customer.country), |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +export const CustomerHelpers = { fromJson, toJson, toJsonUpdate, toJsonCreate } |
| 123 | + |
0 commit comments