Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEC-88] Migrating test data model Inventory Entry #703

Merged
merged 20 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
986b29f
feat(inventory entry): update test data model migrating to new implem…
jmcreasman Oct 31, 2024
0ccb365
feat(inventory entry): add changeset
jmcreasman Oct 31, 2024
561d643
feat(inventory entry): fix error
jmcreasman Oct 31, 2024
7213a80
feat(inventory entry): fix TS errors
jmcreasman Nov 1, 2024
c64602d
feat(inventory entry): fix TS error
jmcreasman Nov 1, 2024
c67857a
feat(inventory entry): update tests
jmcreasman Nov 1, 2024
952ee72
feat(inventory entry): update tests
jmcreasman Nov 1, 2024
9e3eb9a
refactor(inventory-entry): ts types adjustments and supplychannel fie…
CarlosCortizasCT Nov 4, 2024
3df930b
feat(inventory entry): remove unneeded transformer and generator files
jmcreasman Nov 4, 2024
6242dd5
feat(inventory entry): update with migrating the draft
jmcreasman Nov 5, 2024
47cb2dc
feat(inventory entry): remove unused imports
jmcreasman Nov 5, 2024
f4d89be
feat(inventory entry): fix tests
jmcreasman Nov 6, 2024
4b41f56
Update .changeset/metal-mice-sit.md
jmcreasman Nov 6, 2024
cbeea9a
feat(inventory entry): update presets with new implementation part 1
jmcreasman Nov 6, 2024
a08f5d9
feat(inventory entry): update presets with new implementation part 2
jmcreasman Nov 6, 2024
f063954
feat(inventory entry): update based on PR feedback
jmcreasman Nov 6, 2024
8ea98a2
feat(inventory entry): remove commented out code
jmcreasman Nov 7, 2024
4c40fbe
refactor(inventory-entry): refactored sample data presets
CarlosCortizasCT Nov 20, 2024
9dd1d6a
refactor(inventory-entry): reorder presets imports in their barrel files
CarlosCortizasCT Nov 20, 2024
686736a
chore: update readmes of migrated data models
CarlosCortizasCT Nov 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
10 changes: 10 additions & 0 deletions .changeset/metal-mice-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@commercetools-test-data/inventory-entry': minor
---

Refactored `InventoryEntry` and `InventoryEntryDraft` models to use the new implementation patterns.
This means the existing models are now deprecated (though we still support them for backwards compatibility) and consumers should move to the new ones implemented:
* `InventoryEntryRest`
* `InventoryEntryGraphql`
* `InventoryEntryDraftRest`
* `InventoryEntryDraftGraphql`
27 changes: 27 additions & 0 deletions models/channel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,33 @@ $ pnpm add -D @commercetools-test-data/channel

# Usage

```ts
import {
ChannelRest,
ChannelDraftRest,
ChannelGraphql,
ChannelDraftGraphql,
} from '@commercetools-test-data/inventory-entry';

const channelRest = ChannelRest.random().build();
const channelDraftRest = ChannelDraftRest.random().build();

const channelGraphql = ChannelGraphql.random().build();
const channelDraftGraphql = ChannelDraftGraphql.random().build();

// Presets
const withInventorySupplyAndProductDistributionRolesRest =
ChannelDraftRest.presets
.withInventorySupplyAndProductDistributionRoles()
.build();
const withInventorySupplyAndProductDistributionRolesGraphql =
ChannelDraftGraphql.presets
.withInventorySupplyAndProductDistributionRoles()
.build();
```

For backwards compatibility, we still support using the generic data models, but you should consider them legacy as we will be removing them in the future.

```ts
import {
Channel,
Expand Down
17 changes: 17 additions & 0 deletions models/inventory-entry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ $ yarn add -D @commercetools-test-data/inventory-entry

# Usage

```ts
import {
InventoryEntryRest,
InventoryEntryDraftRest,
InventoryEntryGraphql,
InventoryEntryDraftGraphql,
} from '@commercetools-test-data/inventory-entry';

const inventoryEntryRest = InventoryEntryRest.random().build();
const inventoryEntryDraftRest = InventoryEntryDraftRest.random().build();

const inventoryEntryGraphql = InventoryEntryGraphql.random().build();
const inventoryEntryDraftGraphql = InventoryEntryDraftGraphql.random().build();
```

For backwards compatibility, we still support using the generic data models, but you should consider them legacy as we will be removing them in the future.

```ts
import type {
TInventoryEntry,
Expand Down
98 changes: 0 additions & 98 deletions models/inventory-entry/src/builder.spec.ts

This file was deleted.

12 changes: 0 additions & 12 deletions models/inventory-entry/src/builder.ts

This file was deleted.

114 changes: 114 additions & 0 deletions models/inventory-entry/src/builders.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import {
InventoryEntry,
InventoryEntryRest,
InventoryEntryGraphql,
} from './index';

describe('InventoryEntry model builders', () => {
it('builds a REST model', () => {
const restModel = InventoryEntryRest.random().build();

expect(restModel).toEqual(
expect.objectContaining({
id: expect.any(String),
key: expect.any(String),
version: expect.any(Number),
createdAt: expect.any(String),
createdBy: expect.any(Object),
lastModifiedAt: expect.any(String),
lastModifiedBy: expect.any(Object),
sku: expect.any(String),
quantityOnStock: expect.any(Number),
restockableInDays: expect.any(Number),
availableQuantity: expect.any(Number),
expectedDelivery: expect.any(String),
supplyChannel: expect.any(Object),
custom: null,
})
);
});

it('builds a GraphQL model', () => {
const graphqlModel = InventoryEntryGraphql.random().build();

expect(graphqlModel).toMatchObject(
expect.objectContaining({
id: expect.any(String),
key: expect.any(String),
version: expect.any(Number),
createdAt: expect.any(String),
createdBy: expect.any(Object),
lastModifiedAt: expect.any(String),
lastModifiedBy: expect.any(Object),
sku: expect.any(String),
quantityOnStock: expect.any(Number),
restockableInDays: expect.any(Number),
availableQuantity: expect.any(Number),
expectedDelivery: expect.any(String),
supplyChannel: expect.objectContaining({
__typename: 'Channel',
address: expect.objectContaining({
country: expect.any(String),
}),
}),
supplyChannelRef: expect.objectContaining({
__typename: 'Reference',
id: expect.any(String),
typeId: 'channel',
}),
custom: null,
__typename: 'InventoryEntry',
})
);
});
});

describe('InventoryEntry model compatibility builders', () => {
it('builds a REST model', () => {
const restModel = InventoryEntry.random().buildRest();

expect(restModel).toEqual(
expect.objectContaining({
id: expect.any(String),
key: expect.any(String),
version: expect.any(Number),
createdAt: expect.any(String),
createdBy: expect.any(Object),
lastModifiedAt: expect.any(String),
lastModifiedBy: expect.any(Object),
sku: expect.any(String),
quantityOnStock: expect.any(Number),
restockableInDays: expect.any(Number),
availableQuantity: expect.any(Number),
expectedDelivery: expect.any(String),
supplyChannel: expect.any(Object),
custom: null,
})
);
});

it('builds a GraphQL model', () => {
const graphqlModel = InventoryEntry.random().buildGraphql();

expect(graphqlModel).toMatchObject(
expect.objectContaining({
id: expect.any(String),
key: expect.any(String),
version: expect.any(Number),
createdAt: expect.any(String),
createdBy: expect.any(Object),
lastModifiedAt: expect.any(String),
lastModifiedBy: expect.any(Object),
sku: expect.any(String),
availableQuantity: expect.any(Number),
quantityOnStock: expect.any(Number),
restockableInDays: expect.any(Number),
expectedDelivery: expect.any(String),
supplyChannel: expect.objectContaining({
__typename: 'Channel',
}),
custom: null,
})
);
});
});
42 changes: 42 additions & 0 deletions models/inventory-entry/src/builders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
createCompatibilityBuilder,
createSpecializedBuilder,
TModelFieldsConfig,
} from '@commercetools-test-data/core';
import { restFieldsConfig, graphqlFieldsConfig } from './fields-config';
import type {
TCreateInventoryEntryBuilder,
TInventoryEntryRest,
TInventoryEntryGraphql,
} from './types';

export const RestModelBuilder: TCreateInventoryEntryBuilder<
TInventoryEntryRest
> = () =>
createSpecializedBuilder({
name: 'InventoryEntryRestBuilder',
type: 'rest',
modelFieldsConfig: restFieldsConfig,
});

export const GraphqlModelBuilder: TCreateInventoryEntryBuilder<
TInventoryEntryGraphql
> = () =>
createSpecializedBuilder({
name: 'InventoryEntryGraphqlBuilder',
type: 'graphql',
modelFieldsConfig: graphqlFieldsConfig,
});

export const CompatInventoryEntryModelBuilder = <
TInventoryEntryModel extends
| TInventoryEntryRest
| TInventoryEntryGraphql = TInventoryEntryRest,
>() =>
createCompatibilityBuilder<TInventoryEntryModel>({
name: 'InventoryEntryCompatBuilder',
modelFieldsConfig: {
rest: restFieldsConfig as TModelFieldsConfig<TInventoryEntryModel>,
graphql: graphqlFieldsConfig as TModelFieldsConfig<TInventoryEntryModel>,
},
});
56 changes: 56 additions & 0 deletions models/inventory-entry/src/fields-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { ChannelGraphql } from '@commercetools-test-data/channel';
import {
ClientLogging,
Reference,
TReferenceGraphql,
} from '@commercetools-test-data/commons';
import {
fake,
sequence,
type TModelFieldsConfig,
} from '@commercetools-test-data/core';
import { createRelatedDates } from '@commercetools-test-data/utils';
import type { TInventoryEntryGraphql, TInventoryEntryRest } from './types';

const [getOlderDate, getNewerDate, getFutureDate] = createRelatedDates();

const commonFieldsConfig = {
id: fake((f) => f.string.uuid()),
version: sequence(),
createdAt: fake(getOlderDate),
createdBy: fake(() => ClientLogging.random()),
lastModifiedAt: fake(getNewerDate),
lastModifiedBy: fake(() => ClientLogging.random()),
key: fake((f) => f.lorem.slug(2)),
sku: fake((f) => f.lorem.words()),
quantityOnStock: fake((f) => f.number.int()),
availableQuantity: fake((f) => f.number.int()),
restockableInDays: fake((f) => f.number.int()),
expectedDelivery: fake(getFutureDate),
custom: null,
};
export const restFieldsConfig: TModelFieldsConfig<TInventoryEntryRest> = {
fields: {
...commonFieldsConfig,
supplyChannel: fake(() => Reference.presets.channelReference()),
},
};
export const graphqlFieldsConfig: TModelFieldsConfig<TInventoryEntryGraphql> = {
fields: {
...commonFieldsConfig,
__typename: 'InventoryEntry',
supplyChannel: fake(() => ChannelGraphql.random()),
supplyChannelRef: fake((f) => Reference.presets.channelReference()),
},
postBuild: (model) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not 100% sure if I did this part correctly?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me 👍

const supplyChannelRef = model.supplyChannel
? Reference.presets
.channelReference()
.id(model.supplyChannel.id)
.buildGraphql<TReferenceGraphql<'channel'>>()
: null;
return {
supplyChannelRef,
};
},
};
Loading