@loopback/repository-tests 0.20.2 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/dist/crud/relations/acceptance/belongs-to.inclusion-resolver.polymorphic.relation.acceptance.d.ts +2 -0
  2. package/dist/crud/relations/acceptance/belongs-to.inclusion-resolver.polymorphic.relation.acceptance.js +170 -0
  3. package/dist/crud/relations/acceptance/belongs-to.inclusion-resolver.polymorphic.relation.acceptance.js.map +1 -0
  4. package/dist/crud/relations/acceptance/belongs-to.polymorphic.relation.acceptance.d.ts +2 -0
  5. package/dist/crud/relations/acceptance/belongs-to.polymorphic.relation.acceptance.js +55 -0
  6. package/dist/crud/relations/acceptance/belongs-to.polymorphic.relation.acceptance.js.map +1 -0
  7. package/dist/crud/relations/acceptance/has-many-through-inclusion-resolver.polymorphic.acceptance.d.ts +2 -0
  8. package/dist/crud/relations/acceptance/has-many-through-inclusion-resolver.polymorphic.acceptance.js +170 -0
  9. package/dist/crud/relations/acceptance/has-many-through-inclusion-resolver.polymorphic.acceptance.js.map +1 -0
  10. package/dist/crud/relations/acceptance/has-many-through.relation.polymorphic.acceptance.d.ts +2 -0
  11. package/dist/crud/relations/acceptance/has-many-through.relation.polymorphic.acceptance.js +361 -0
  12. package/dist/crud/relations/acceptance/has-many-through.relation.polymorphic.acceptance.js.map +1 -0
  13. package/dist/crud/relations/acceptance/has-one.inclusion-resolver.polymorphic.acceptance.d.ts +2 -0
  14. package/dist/crud/relations/acceptance/has-one.inclusion-resolver.polymorphic.acceptance.js +161 -0
  15. package/dist/crud/relations/acceptance/has-one.inclusion-resolver.polymorphic.acceptance.js.map +1 -0
  16. package/dist/crud/relations/acceptance/has-one.relation.polymorphic.acceptance.d.ts +2 -0
  17. package/dist/crud/relations/acceptance/has-one.relation.polymorphic.acceptance.js +190 -0
  18. package/dist/crud/relations/acceptance/has-one.relation.polymorphic.acceptance.js.map +1 -0
  19. package/dist/crud/relations/fixtures/models/contact.model.d.ts +18 -0
  20. package/dist/crud/relations/fixtures/models/contact.model.js +49 -0
  21. package/dist/crud/relations/fixtures/models/contact.model.js.map +1 -0
  22. package/dist/crud/relations/fixtures/models/customer-promotion-link.model.d.ts +14 -0
  23. package/dist/crud/relations/fixtures/models/customer-promotion-link.model.js +46 -0
  24. package/dist/crud/relations/fixtures/models/customer-promotion-link.model.js.map +1 -0
  25. package/dist/crud/relations/fixtures/models/customer.model.d.ts +16 -3
  26. package/dist/crud/relations/fixtures/models/customer.model.js +27 -9
  27. package/dist/crud/relations/fixtures/models/customer.model.js.map +1 -1
  28. package/dist/crud/relations/fixtures/models/index.d.ts +6 -0
  29. package/dist/crud/relations/fixtures/models/index.js +6 -0
  30. package/dist/crud/relations/fixtures/models/index.js.map +1 -1
  31. package/dist/crud/relations/fixtures/models/payment-method.model.d.ts +47 -0
  32. package/dist/crud/relations/fixtures/models/payment-method.model.js +87 -0
  33. package/dist/crud/relations/fixtures/models/payment-method.model.js.map +1 -0
  34. package/dist/crud/relations/fixtures/models/promotion.model.d.ts +27 -0
  35. package/dist/crud/relations/fixtures/models/promotion.model.js +54 -0
  36. package/dist/crud/relations/fixtures/models/promotion.model.js.map +1 -0
  37. package/dist/crud/relations/fixtures/models/stakeholder.model.d.ts +7 -0
  38. package/dist/crud/relations/fixtures/models/stakeholder.model.js +38 -0
  39. package/dist/crud/relations/fixtures/models/stakeholder.model.js.map +1 -0
  40. package/dist/crud/relations/fixtures/models/supplier.model.d.ts +15 -0
  41. package/dist/crud/relations/fixtures/models/supplier.model.js +23 -0
  42. package/dist/crud/relations/fixtures/models/supplier.model.js.map +1 -0
  43. package/dist/crud/relations/fixtures/repositories/contact.repository.d.ts +30 -0
  44. package/dist/crud/relations/fixtures/repositories/contact.repository.js +21 -0
  45. package/dist/crud/relations/fixtures/repositories/contact.repository.js.map +1 -0
  46. package/dist/crud/relations/fixtures/repositories/customer-promotion-link.repository.d.ts +26 -0
  47. package/dist/crud/relations/fixtures/repositories/customer-promotion-link.repository.js +18 -0
  48. package/dist/crud/relations/fixtures/repositories/customer-promotion-link.repository.js.map +1 -0
  49. package/dist/crud/relations/fixtures/repositories/customer.repository.d.ts +9 -2
  50. package/dist/crud/relations/fixtures/repositories/customer.repository.js +7 -1
  51. package/dist/crud/relations/fixtures/repositories/customer.repository.js.map +1 -1
  52. package/dist/crud/relations/fixtures/repositories/index.d.ts +6 -1
  53. package/dist/crud/relations/fixtures/repositories/index.js +6 -1
  54. package/dist/crud/relations/fixtures/repositories/index.js.map +1 -1
  55. package/dist/crud/relations/fixtures/repositories/payment-method.repository.d.ts +77 -0
  56. package/dist/crud/relations/fixtures/repositories/payment-method.repository.js +45 -0
  57. package/dist/crud/relations/fixtures/repositories/payment-method.repository.js.map +1 -0
  58. package/dist/crud/relations/fixtures/repositories/promotion.repository.d.ts +49 -0
  59. package/dist/crud/relations/fixtures/repositories/promotion.repository.js +25 -0
  60. package/dist/crud/relations/fixtures/repositories/promotion.repository.js.map +1 -0
  61. package/dist/crud/relations/fixtures/repositories/supplier.repository.d.ts +28 -0
  62. package/dist/crud/relations/fixtures/repositories/supplier.repository.js +21 -0
  63. package/dist/crud/relations/fixtures/repositories/supplier.repository.js.map +1 -0
  64. package/dist/crud/relations/helpers.d.ts +9 -1
  65. package/dist/crud/relations/helpers.js +85 -10
  66. package/dist/crud/relations/helpers.js.map +1 -1
  67. package/dist/helpers.repository-tests.d.ts +1 -1
  68. package/dist/helpers.repository-tests.js.map +1 -1
  69. package/package.json +12 -12
  70. package/src/crud/relations/acceptance/belongs-to.inclusion-resolver.polymorphic.relation.acceptance.ts +221 -0
  71. package/src/crud/relations/acceptance/belongs-to.polymorphic.relation.acceptance.ts +88 -0
  72. package/src/crud/relations/acceptance/has-many-through-inclusion-resolver.polymorphic.acceptance.ts +255 -0
  73. package/src/crud/relations/acceptance/has-many-through.relation.polymorphic.acceptance.ts +494 -0
  74. package/src/crud/relations/acceptance/has-one.inclusion-resolver.polymorphic.acceptance.ts +208 -0
  75. package/src/crud/relations/acceptance/has-one.relation.polymorphic.acceptance.ts +328 -0
  76. package/src/crud/relations/fixtures/models/contact.model.ts +56 -0
  77. package/src/crud/relations/fixtures/models/customer-promotion-link.model.ts +52 -0
  78. package/src/crud/relations/fixtures/models/customer.model.ts +39 -9
  79. package/src/crud/relations/fixtures/models/index.ts +6 -0
  80. package/src/crud/relations/fixtures/models/payment-method.model.ts +118 -0
  81. package/src/crud/relations/fixtures/models/promotion.model.ts +67 -0
  82. package/src/crud/relations/fixtures/models/stakeholder.model.ts +33 -0
  83. package/src/crud/relations/fixtures/models/supplier.model.ts +35 -0
  84. package/src/crud/relations/fixtures/repositories/contact.repository.ts +44 -0
  85. package/src/crud/relations/fixtures/repositories/customer-promotion-link.repository.ts +20 -0
  86. package/src/crud/relations/fixtures/repositories/customer.repository.ts +47 -0
  87. package/src/crud/relations/fixtures/repositories/index.ts +6 -1
  88. package/src/crud/relations/fixtures/repositories/payment-method.repository.ts +118 -0
  89. package/src/crud/relations/fixtures/repositories/promotion.repository.ts +37 -0
  90. package/src/crud/relations/fixtures/repositories/supplier.repository.ts +40 -0
  91. package/src/crud/relations/helpers.ts +200 -30
  92. package/src/helpers.repository-tests.ts +1 -1
@@ -0,0 +1,328 @@
1
+ // Copyright IBM Corp. 2019,2020. All Rights Reserved.
2
+ // Node module: @loopback/repository-tests
3
+ // This file is licensed under the MIT License.
4
+ // License text available at https://opensource.org/licenses/MIT
5
+
6
+ import {DataObject, EntityNotFoundError} from '@loopback/repository';
7
+ import {expect, toJSON} from '@loopback/testlab';
8
+ import {
9
+ CrudFeatures,
10
+ CrudRepositoryCtor,
11
+ CrudTestContext,
12
+ DataSourceOptions,
13
+ } from '../../..';
14
+ import {
15
+ deleteAllModelsInDefaultDataSource,
16
+ MixedIdType,
17
+ withCrudCtx,
18
+ } from '../../../helpers.repository-tests';
19
+ import {
20
+ Cash,
21
+ CashRepository,
22
+ CreditCard,
23
+ CreditCardRepository,
24
+ Customer,
25
+ CustomerRepository,
26
+ PaymentMethod,
27
+ } from '../fixtures/models';
28
+ import {givenBoundCrudRepositories} from '../helpers';
29
+
30
+ export function hasOneRelationPolymorphicAcceptance(
31
+ dataSourceOptions: DataSourceOptions,
32
+ repositoryClass: CrudRepositoryCtor,
33
+ features: CrudFeatures,
34
+ ) {
35
+ describe('hasOne relation polymorphic (acceptance)', () => {
36
+ let customerRepo: CustomerRepository;
37
+ let creditCardRepo: CreditCardRepository;
38
+ let cashRepo: CashRepository;
39
+ let existingCustomerId: MixedIdType;
40
+
41
+ before(deleteAllModelsInDefaultDataSource);
42
+
43
+ before(
44
+ withCrudCtx(async function setupRepository(ctx: CrudTestContext) {
45
+ ({customerRepo, creditCardRepo, cashRepo} = givenBoundCrudRepositories(
46
+ ctx.dataSource,
47
+ repositoryClass,
48
+ features,
49
+ ));
50
+ const models = [Customer, CreditCard, Cash];
51
+ await ctx.dataSource.automigrate(models.map(m => m.name));
52
+ }),
53
+ );
54
+
55
+ beforeEach(async () => {
56
+ await customerRepo.deleteAll();
57
+ await creditCardRepo.deleteAll();
58
+ await cashRepo.deleteAll();
59
+ existingCustomerId = (await givenPersistedCustomerInstance()).id;
60
+ });
61
+
62
+ it('can create an instance of the related model', async () => {
63
+ const address = await createCustomerPaymentMethod(
64
+ existingCustomerId,
65
+ {},
66
+ 'CreditCard',
67
+ );
68
+ expect(toJSON(address)).to.containDeep(
69
+ toJSON({
70
+ customerId: existingCustomerId,
71
+ }),
72
+ );
73
+
74
+ const persisted = await creditCardRepo.findById(address.id);
75
+ expect(toJSON(persisted)).to.deepEqual(
76
+ toJSON({
77
+ ...address,
78
+ zipcode: features.emptyValue,
79
+ city: features.emptyValue,
80
+ province: features.emptyValue,
81
+ }),
82
+ );
83
+ });
84
+
85
+ // We do not enforce referential integrity at the moment. It is up to
86
+ // our users to set up unique constraint(s) between related models at the
87
+ // database level
88
+ it.skip('refuses to create related model instance twice', async () => {
89
+ const creditCard = await createCustomerPaymentMethod(
90
+ existingCustomerId,
91
+ {},
92
+ 'CreditCard',
93
+ );
94
+ await expect(
95
+ createCustomerPaymentMethod(existingCustomerId, {}, 'Cash'),
96
+ ).to.be.rejectedWith(/Duplicate entry for PaymentMethod.customerId/);
97
+ expect(creditCard.toObject()).to.containDeep({
98
+ customerId: existingCustomerId,
99
+ });
100
+
101
+ const persisted = await creditCardRepo.findById(creditCard.id);
102
+ expect(persisted.toObject()).to.deepEqual(creditCard.toObject());
103
+ });
104
+
105
+ it('can find instance of the related model', async () => {
106
+ const creditCard = await createCustomerPaymentMethod(
107
+ existingCustomerId,
108
+ {},
109
+ 'CreditCard',
110
+ );
111
+ const foundCreditCard = await findCustomerPaymentMethod(
112
+ existingCustomerId,
113
+ 'CreditCard',
114
+ );
115
+ expect(toJSON(foundCreditCard)).to.containEql(toJSON(creditCard));
116
+ expect(toJSON(foundCreditCard)).to.deepEqual(
117
+ toJSON({
118
+ ...creditCard,
119
+ }),
120
+ );
121
+
122
+ const persisted = await creditCardRepo.find({
123
+ where: {customerId: existingCustomerId},
124
+ });
125
+ expect(persisted[0]).to.deepEqual(foundCreditCard);
126
+ });
127
+
128
+ it('reports EntityNotFound error when related model is deleted', async () => {
129
+ const paymentMethod = await createCustomerPaymentMethod(
130
+ existingCustomerId,
131
+ {},
132
+ 'CreditCard',
133
+ );
134
+ await creditCardRepo.deleteById(paymentMethod.id);
135
+
136
+ await expect(
137
+ findCustomerPaymentMethod(existingCustomerId, 'CreditCard'),
138
+ ).to.be.rejectedWith(EntityNotFoundError);
139
+ });
140
+
141
+ it('can PATCH hasOne instances', async () => {
142
+ const paymentMethod = await createCustomerPaymentMethod(
143
+ existingCustomerId,
144
+ {},
145
+ 'CreditCard',
146
+ );
147
+
148
+ const patchObject = {cardNumber: 46458921} as DataObject<PaymentMethod>;
149
+ const arePatched = await patchCustomerPaymentMethod(
150
+ existingCustomerId,
151
+ {CreditCard: patchObject as DataObject<CreditCard>} as {
152
+ [polymorphicType: string]: DataObject<CreditCard>;
153
+ },
154
+ true,
155
+ );
156
+
157
+ expect(arePatched).to.deepEqual({count: 1});
158
+ const patchedData = await creditCardRepo.findById(paymentMethod.id);
159
+
160
+ expect(toJSON(patchedData)).to.deepEqual(
161
+ toJSON({
162
+ id: paymentMethod.id,
163
+ customerId: existingCustomerId,
164
+ cardNumber: 46458921,
165
+ }),
166
+ );
167
+ });
168
+
169
+ it('patches the related instance only', async () => {
170
+ const bob = await customerRepo.create({name: 'Bob'});
171
+ await customerRepo
172
+ .paymentMethod(bob.id)
173
+ .create({cardNumber: 12345} as DataObject<PaymentMethod>, {
174
+ polymorphicType: 'CreditCard',
175
+ });
176
+
177
+ const alice = await customerRepo.create({name: 'Alice'});
178
+ await customerRepo
179
+ .paymentMethod(alice.id)
180
+ .create({cardNumber: 67890} as DataObject<PaymentMethod>, {
181
+ polymorphicType: 'CreditCard',
182
+ });
183
+
184
+ const result = await patchCustomerPaymentMethod(
185
+ alice.id,
186
+ {
187
+ CreditCard: {
188
+ cardNumber: 7894512,
189
+ } as DataObject<CreditCard>,
190
+ } as {[polymorphicType: string]: DataObject<CreditCard>},
191
+ true,
192
+ );
193
+
194
+ expect(result).to.deepEqual({count: 1});
195
+
196
+ const foundBob = await customerRepo
197
+ .paymentMethod(bob.id)
198
+ .get(undefined, {polymorphicType: 'CreditCard'});
199
+ expect(toJSON(foundBob)).to.containDeep({cardNumber: 12345});
200
+
201
+ const foundAlice = await customerRepo
202
+ .paymentMethod(bob.id)
203
+ .get(undefined, {polymorphicType: 'CreditCard'});
204
+ expect(toJSON(foundAlice)).to.containDeep({cardNumber: 12345});
205
+ });
206
+
207
+ it('throws an error when PATCH tries to change the foreignKey', async () => {
208
+ const anotherId = (await givenPersistedCustomerInstance()).id;
209
+ await expect(
210
+ patchCustomerPaymentMethod(
211
+ existingCustomerId,
212
+ {
213
+ CreditCard: {
214
+ customerId: anotherId,
215
+ } as DataObject<CreditCard>,
216
+ } as {[polymorphicType: string]: DataObject<CreditCard>},
217
+ true,
218
+ ),
219
+ ).to.be.rejectedWith(/Property "customerId" cannot be changed!/);
220
+ });
221
+
222
+ it('can DELETE hasOne relation instances', async () => {
223
+ await createCustomerPaymentMethod(existingCustomerId, {}, 'CreditCard');
224
+
225
+ const areDeleted = await deleteCustomerPaymentMethod(
226
+ existingCustomerId,
227
+ 'CreditCard',
228
+ );
229
+ expect(areDeleted).to.deepEqual({count: 1});
230
+
231
+ await expect(
232
+ findCustomerPaymentMethod(existingCustomerId, 'CreditCard'),
233
+ ).to.be.rejectedWith(EntityNotFoundError);
234
+ });
235
+
236
+ it('deletes the related model instance only', async () => {
237
+ const bob = await customerRepo.create({name: 'Bob'});
238
+ await customerRepo
239
+ .paymentMethod(bob.id)
240
+ .create({}, {polymorphicType: 'CreditCard'});
241
+
242
+ const alice = await customerRepo.create({name: 'Alice'});
243
+ await customerRepo
244
+ .paymentMethod(alice.id)
245
+ .create({}, {polymorphicType: 'Cash'});
246
+
247
+ const brown = await customerRepo.create({name: 'Brown'});
248
+ await customerRepo
249
+ .paymentMethod(brown.id)
250
+ .create({}, {polymorphicType: 'Cash'});
251
+
252
+ const result = await deleteCustomerPaymentMethod(alice.id, 'Cash');
253
+
254
+ expect(result).to.deepEqual({count: 1});
255
+
256
+ const found = await cashRepo.find();
257
+ expect(found).to.have.length(1);
258
+ });
259
+
260
+ it('deletes the related model instance with wrong type', async () => {
261
+ const bob = await customerRepo.create({name: 'Bob'});
262
+ await customerRepo
263
+ .paymentMethod(bob.id)
264
+ .create({}, {polymorphicType: 'CreditCard'});
265
+
266
+ const alice = await customerRepo.create({name: 'Alice'});
267
+ await customerRepo
268
+ .paymentMethod(alice.id)
269
+ .create({}, {polymorphicType: 'Cash'});
270
+
271
+ const brown = await customerRepo.create({name: 'Brown'});
272
+ await customerRepo
273
+ .paymentMethod(brown.id)
274
+ .create({}, {polymorphicType: 'Cash'});
275
+
276
+ const result = await deleteCustomerPaymentMethod(alice.id, 'CreditCard');
277
+
278
+ expect(result).to.deepEqual({count: 0});
279
+
280
+ const found = await cashRepo.find();
281
+ expect(found).to.have.length(2);
282
+ });
283
+
284
+ /*---------------- HELPERS -----------------*/
285
+
286
+ async function createCustomerPaymentMethod(
287
+ customerId: MixedIdType,
288
+ paymentMethodData: DataObject<PaymentMethod>,
289
+ polymorphicTypeName: string,
290
+ ): Promise<PaymentMethod> {
291
+ return customerRepo
292
+ .paymentMethod(customerId)
293
+ .create(paymentMethodData, {polymorphicType: polymorphicTypeName});
294
+ }
295
+
296
+ async function findCustomerPaymentMethod(
297
+ customerId: MixedIdType,
298
+ polymorphicType: string,
299
+ ) {
300
+ return customerRepo
301
+ .paymentMethod(customerId)
302
+ .get(undefined, {polymorphicType: polymorphicType});
303
+ }
304
+
305
+ async function patchCustomerPaymentMethod(
306
+ customerId: MixedIdType,
307
+ paymentMethodData: DataObject<PaymentMethod>,
308
+ isMultipleTypes: boolean,
309
+ ) {
310
+ return customerRepo
311
+ .paymentMethod(customerId)
312
+ .patch(paymentMethodData, {isPolymorphic: isMultipleTypes});
313
+ }
314
+
315
+ async function deleteCustomerPaymentMethod(
316
+ customerId: MixedIdType,
317
+ polymorphicType: string,
318
+ ) {
319
+ return customerRepo
320
+ .paymentMethod(customerId)
321
+ .delete({polymorphicType: polymorphicType});
322
+ }
323
+
324
+ async function givenPersistedCustomerInstance() {
325
+ return customerRepo.create({name: 'a customer'});
326
+ }
327
+ });
328
+ }
@@ -0,0 +1,56 @@
1
+ // Copyright IBM Corp. 2019. All Rights Reserved.
2
+ // Node module: @loopback/repository-tests
3
+ // This file is licensed under the MIT License.
4
+ // License text available at https://opensource.org/licenses/MIT
5
+
6
+ import {
7
+ belongsTo,
8
+ BelongsToAccessor,
9
+ Entity,
10
+ EntityCrudRepository,
11
+ model,
12
+ property,
13
+ } from '@loopback/repository';
14
+ import {MixedIdType} from '../../../../helpers.repository-tests';
15
+ import {CustomerWithRelations} from './customer.model';
16
+ import {Stakeholder} from './stakeholder.model';
17
+
18
+ @model()
19
+ export class Contact extends Entity {
20
+ @property({
21
+ id: true,
22
+ generated: true,
23
+ useDefaultIdType: true,
24
+ })
25
+ id: MixedIdType;
26
+ @property({
27
+ type: 'string',
28
+ })
29
+ name: string;
30
+ @property({
31
+ type: 'string',
32
+ })
33
+ email: string;
34
+
35
+ @belongsTo(() => Stakeholder, {polymorphic: true})
36
+ stakeholderId: MixedIdType;
37
+
38
+ @property({
39
+ type: 'string',
40
+ required: true,
41
+ default: 'Customer',
42
+ })
43
+ stakeholderType: string;
44
+ }
45
+
46
+ export interface ContactRelations {
47
+ stakeholder?: CustomerWithRelations;
48
+ }
49
+
50
+ export type ContactWithRelations = Contact & ContactRelations;
51
+
52
+ export interface ContactRepository
53
+ extends EntityCrudRepository<Contact, typeof Contact.prototype.id> {
54
+ // define additional members like relation methods here
55
+ stakeholder: BelongsToAccessor<Stakeholder, MixedIdType>;
56
+ }
@@ -0,0 +1,52 @@
1
+ // Copyright IBM Corp. 2020. All Rights Reserved.
2
+ // Node module: @loopback/repository-tests
3
+ // This file is licensed under the MIT License.
4
+ // License text available at https://opensource.org/licenses/MIT
5
+
6
+ import {
7
+ Entity,
8
+ EntityCrudRepository,
9
+ model,
10
+ property,
11
+ } from '@loopback/repository';
12
+ import {MixedIdType} from '../../../../helpers.repository-tests';
13
+
14
+ @model()
15
+ export class CustomerPromotionLink extends Entity {
16
+ @property({
17
+ id: true,
18
+ generated: true,
19
+ useDefaultIdType: true,
20
+ })
21
+ id: MixedIdType;
22
+
23
+ @property()
24
+ customerId: MixedIdType;
25
+
26
+ @property()
27
+ // eslint-disable-next-line @typescript-eslint/naming-convention
28
+ promotion_id: MixedIdType;
29
+
30
+ @property({
31
+ type: 'string',
32
+ default: 'HalfPrice',
33
+ required: true,
34
+ })
35
+ promotiontype: string;
36
+
37
+ @property({
38
+ type: 'string',
39
+ })
40
+ description?: string;
41
+ }
42
+
43
+ export interface CustomerPromotionLinkRelations {}
44
+
45
+ export type CustomerPromotionLinkWithRelations = CustomerPromotionLink &
46
+ CustomerPromotionLinkRelations;
47
+
48
+ export interface CustomerPromotionLinkRepository
49
+ extends EntityCrudRepository<
50
+ CustomerPromotionLink,
51
+ typeof CustomerPromotionLink.prototype.id
52
+ > {}
@@ -5,7 +5,6 @@
5
5
 
6
6
  import {
7
7
  belongsTo,
8
- Entity,
9
8
  EntityCrudRepository,
10
9
  hasMany,
11
10
  HasManyRepositoryFactory,
@@ -19,18 +18,19 @@ import {BelongsToAccessor} from '@loopback/repository/src';
19
18
  import {MixedIdType} from '../../../../helpers.repository-tests';
20
19
  import {Address, AddressWithRelations} from './address.model';
21
20
  import {CartItem, CartItemWithRelations} from './cart-item.model';
21
+ import {Contact, ContactWithRelations} from './contact.model';
22
22
  import {CustomerCartItemLink} from './customer-cart-item-link.model';
23
+ import {CustomerPromotionLink} from './customer-promotion-link.model';
23
24
  import {Order, OrderWithRelations} from './order.model';
25
+ import {
26
+ PaymentMethod,
27
+ PaymentMethodWithRelations,
28
+ } from './payment-method.model';
29
+ import {Promotion, PromotionWithRelations} from './promotion.model';
30
+ import {Stakeholder} from './stakeholder.model';
24
31
 
25
32
  @model()
26
- export class Customer extends Entity {
27
- @property({
28
- id: true,
29
- generated: true,
30
- useDefaultIdType: true,
31
- })
32
- id: MixedIdType;
33
-
33
+ export class Customer extends Stakeholder {
34
34
  @property({
35
35
  type: 'string',
36
36
  })
@@ -50,6 +50,25 @@ export class Customer extends Entity {
50
50
 
51
51
  @hasMany(() => CartItem, {through: {model: () => CustomerCartItemLink}})
52
52
  cartItems: CartItem[];
53
+
54
+ @hasMany(() => Promotion, {
55
+ through: {
56
+ model: () => CustomerPromotionLink,
57
+ keyTo: 'promotion_id',
58
+ polymorphic: {discriminator: 'promotiontype'},
59
+ },
60
+ })
61
+ promotions: Promotion[];
62
+
63
+ @hasOne(() => PaymentMethod, {polymorphic: true})
64
+ paymentMethod: PaymentMethod;
65
+
66
+ @property({
67
+ type: 'string',
68
+ required: true,
69
+ default: 'CreditCard',
70
+ })
71
+ paymentMethodType: string;
53
72
  }
54
73
 
55
74
  export interface CustomerRelations {
@@ -58,6 +77,9 @@ export interface CustomerRelations {
58
77
  customers?: CustomerWithRelations[];
59
78
  parentCustomer?: CustomerWithRelations;
60
79
  cartItems?: CartItemWithRelations[];
80
+ paymentMethod: PaymentMethodWithRelations;
81
+ promotions?: PromotionWithRelations[];
82
+ contact?: ContactWithRelations;
61
83
  }
62
84
 
63
85
  export type CustomerWithRelations = Customer & CustomerRelations;
@@ -75,4 +97,12 @@ export interface CustomerRepository
75
97
  CustomerCartItemLink,
76
98
  MixedIdType
77
99
  >;
100
+ promotions: HasManyThroughRepositoryFactory<
101
+ Promotion,
102
+ MixedIdType,
103
+ CustomerPromotionLink,
104
+ MixedIdType
105
+ >;
106
+ paymentMethod: HasOneRepositoryFactory<PaymentMethod, MixedIdType>;
107
+ contact: HasOneRepositoryFactory<Contact, MixedIdType>;
78
108
  }
@@ -5,9 +5,15 @@
5
5
 
6
6
  export * from './address.model';
7
7
  export * from './cart-item.model';
8
+ export * from './contact.model';
8
9
  export * from './customer-cart-item-link.model';
10
+ export * from './customer-promotion-link.model';
9
11
  export * from './customer.model';
10
12
  export * from './order.model';
13
+ export * from './payment-method.model';
14
+ export * from './promotion.model';
11
15
  export * from './shipment.model';
16
+ export * from './stakeholder.model';
17
+ export * from './supplier.model';
12
18
  export * from './user-link.model';
13
19
  export * from './user.model';
@@ -0,0 +1,118 @@
1
+ // Copyright IBM Corp. 2019,2020. All Rights Reserved.
2
+ // Node module: @loopback/repository-tests
3
+ // This file is licensed under the MIT License.
4
+ // License text available at https://opensource.org/licenses/MIT
5
+
6
+ import {
7
+ belongsTo,
8
+ Entity,
9
+ EntityCrudRepository,
10
+ hasOne,
11
+ HasOneRepositoryFactory,
12
+ model,
13
+ property,
14
+ } from '@loopback/repository';
15
+ import {BelongsToAccessor} from '@loopback/repository/src';
16
+ import {Customer, CustomerWithRelations} from '.';
17
+ import {MixedIdType} from '../../../../helpers.repository-tests';
18
+ import {Stakeholder} from './stakeholder.model';
19
+
20
+ @model()
21
+ export class PaymentMethod extends Entity {
22
+ @property({
23
+ id: true,
24
+ generated: true,
25
+ useDefaultIdType: true,
26
+ })
27
+ id: MixedIdType;
28
+
29
+ @belongsTo(() => Stakeholder)
30
+ customerId: MixedIdType;
31
+ }
32
+
33
+ export interface PaymentMethodRelations {
34
+ customer?: CustomerWithRelations[];
35
+ }
36
+
37
+ export type PaymentMethodWithRelations = PaymentMethod & PaymentMethodRelations;
38
+
39
+ @model()
40
+ export class CardInfo extends Entity {
41
+ @property({
42
+ id: true,
43
+ generated: true,
44
+ useDefaultIdType: true,
45
+ })
46
+ id: MixedIdType;
47
+
48
+ @belongsTo(() => CreditCard)
49
+ creditCardId: MixedIdType;
50
+
51
+ @property({
52
+ type: 'string',
53
+ required: true,
54
+ default: 'default holder',
55
+ })
56
+ cardHolder: string;
57
+ }
58
+
59
+ export interface CardInfoRelations {
60
+ creditCard?: CreditCardWithRelations[];
61
+ }
62
+
63
+ export type CardInfoWithRelations = CardInfo & CardInfoRelations;
64
+
65
+ @model()
66
+ export class CreditCard extends PaymentMethod {
67
+ @property({
68
+ type: 'number',
69
+ required: true,
70
+ default: 0,
71
+ })
72
+ cardNumber: number;
73
+
74
+ @hasOne(() => CardInfo)
75
+ cardInfo: CardInfo;
76
+ }
77
+
78
+ export interface CreditCardRelations {
79
+ customer?: CustomerWithRelations[];
80
+ cardInfo: CardInfoWithRelations;
81
+ }
82
+
83
+ export type CreditCardWithRelations = CreditCard & CreditCardRelations;
84
+
85
+ @model()
86
+ export class Cash extends PaymentMethod {
87
+ @property({
88
+ type: 'string',
89
+ required: true,
90
+ default: 'default cash detail',
91
+ })
92
+ detail: string;
93
+ }
94
+
95
+ export interface CashRelations {
96
+ customer?: CustomerWithRelations[];
97
+ }
98
+
99
+ export type CashWithRelations = Cash & CashRelations;
100
+
101
+ export interface CreditCardRepository
102
+ extends EntityCrudRepository<CreditCard, typeof CreditCard.prototype.id> {
103
+ // define additional members like relation methods here
104
+ customer: BelongsToAccessor<Customer, MixedIdType>;
105
+ cardInfo: HasOneRepositoryFactory<CardInfo, MixedIdType>;
106
+ }
107
+
108
+ export interface CashRepository
109
+ extends EntityCrudRepository<Cash, typeof Cash.prototype.id> {
110
+ // define additional members like relation methods here
111
+ customer: BelongsToAccessor<Customer, MixedIdType>;
112
+ }
113
+
114
+ export interface CardInfoRepository
115
+ extends EntityCrudRepository<CardInfo, typeof CardInfo.prototype.id> {
116
+ // define additional members like relation methods here
117
+ creditCard: BelongsToAccessor<CreditCard, MixedIdType>;
118
+ }