-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathcustomer.model.ts
108 lines (95 loc) · 3.13 KB
/
customer.model.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright IBM Corp. and LoopBack contributors 2019,2020. All Rights Reserved.
// Node module: @loopback/repository-tests
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {
belongsTo,
EntityCrudRepository,
hasMany,
HasManyRepositoryFactory,
HasManyThroughRepositoryFactory,
hasOne,
HasOneRepositoryFactory,
model,
property,
} from '@loopback/repository';
import {BelongsToAccessor} from '@loopback/repository/src';
import {MixedIdType} from '../../../../helpers.repository-tests';
import {Address, AddressWithRelations} from './address.model';
import {CartItem, CartItemWithRelations} from './cart-item.model';
import {Contact, ContactWithRelations} from './contact.model';
import {CustomerCartItemLink} from './customer-cart-item-link.model';
import {CustomerPromotionLink} from './customer-promotion-link.model';
import {Order, OrderWithRelations} from './order.model';
import {
PaymentMethod,
PaymentMethodWithRelations,
} from './payment-method.model';
import {Promotion, PromotionWithRelations} from './promotion.model';
import {Stakeholder} from './stakeholder.model';
@model()
export class Customer extends Stakeholder {
@property({
type: 'string',
})
name: string;
@hasMany(() => Order)
orders: Order[];
@hasOne(() => Address)
address: Address;
@hasMany(() => Customer, {keyTo: 'parentId'})
customers?: Customer[];
@belongsTo(() => Customer)
parentId?: MixedIdType;
@hasMany(() => CartItem, {through: {model: () => CustomerCartItemLink}})
cartItems: CartItem[];
@hasMany(() => Promotion, {
through: {
model: () => CustomerPromotionLink,
keyTo: 'promotion_id',
polymorphic: {discriminator: 'promotiontype'},
},
})
promotions: Promotion[];
@hasOne(() => PaymentMethod, {polymorphic: true})
paymentMethod: PaymentMethod;
@property({
type: 'string',
required: true,
default: 'CreditCard',
})
paymentMethodType: string;
}
export interface CustomerRelations {
address?: AddressWithRelations;
orders?: OrderWithRelations[];
customers?: CustomerWithRelations[];
parentCustomer?: CustomerWithRelations;
cartItems?: CartItemWithRelations[];
paymentMethod: PaymentMethodWithRelations;
promotions?: PromotionWithRelations[];
contact?: ContactWithRelations;
}
export type CustomerWithRelations = Customer & CustomerRelations;
export interface CustomerRepository
extends EntityCrudRepository<Customer, typeof Customer.prototype.id> {
// define additional members like relation methods here
address: HasOneRepositoryFactory<Address, MixedIdType>;
orders: HasManyRepositoryFactory<Order, MixedIdType>;
customers: HasManyRepositoryFactory<Customer, MixedIdType>;
parent: BelongsToAccessor<Customer, MixedIdType>;
cartItems: HasManyThroughRepositoryFactory<
CartItem,
MixedIdType,
CustomerCartItemLink,
MixedIdType
>;
promotions: HasManyThroughRepositoryFactory<
Promotion,
MixedIdType,
CustomerPromotionLink,
MixedIdType
>;
paymentMethod: HasOneRepositoryFactory<PaymentMethod, MixedIdType>;
contact: HasOneRepositoryFactory<Contact, MixedIdType>;
}