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

FCT-702: add Associate and Associate-Role-Assignment models #481

Merged
merged 7 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/polite-ladybugs-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools-test-data/business-unit': minor
---

Add `Associate` and `AssociateRoleAssignment` models, adjust types and constants colocation.
9 changes: 9 additions & 0 deletions models/business-unit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,12 @@ import { CompanyDraft, DivisionDraft, type TBusinessUnitDraft } from from '@comm
const companyDraft = CompanyDraft.random().build<TBusinessUnitDraft>();
const divisionDraft = DivisionDraft.random().build<TBusinessUnitDraft>();
```

## Associate and AssociateRoleAssignment

```ts
import { Associate, AssociateRoleAssignment, type TAssociateDefault, type TAssociateRoleAssignmentDefault } from from '@commercetools-test-data/business-unit';

const associate = Associate.random().build<TAssociateDefault>();
const associateRoleAssignment = AssociateRoleAssignment.random().build<TAssociateRoleAssignmentDefault>();
```
1 change: 1 addition & 0 deletions models/business-unit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"dependencies": {
"@babel/runtime": "^7.17.9",
"@babel/runtime-corejs3": "^7.17.9",
"@commercetools-test-data/associate-role": "7.0.0",
"@commercetools-test-data/commons": "7.0.0",
"@commercetools-test-data/core": "7.0.0",
"@commercetools-test-data/utils": "7.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* eslint-disable jest/no-disabled-tests */
/* eslint-disable jest/valid-title */
import { createBuilderSpec } from '@commercetools-test-data/core/test-utils';
import {
TAssociateRoleAssignmentDraft,
TAssociateRoleAssignmentDraftGraphql,
} from '../types';
import * as AssociateRoleAssignmentDraft from '.';

describe('builder', () => {
it(
...createBuilderSpec<
TAssociateRoleAssignmentDraft,
TAssociateRoleAssignmentDraft
>(
'default',
AssociateRoleAssignmentDraft.random(),
expect.objectContaining({
associateRole: expect.objectContaining({
typeId: 'associate-role',
id: expect.any(String),
}),
inheritance: expect.any(String),
})
)
);

it(
...createBuilderSpec<
TAssociateRoleAssignmentDraft,
TAssociateRoleAssignmentDraft
>(
'rest',
AssociateRoleAssignmentDraft.random(),
expect.objectContaining({
associateRole: expect.objectContaining({
typeId: 'associate-role',
id: expect.any(String),
}),
inheritance: expect.any(String),
})
)
);

it(
...createBuilderSpec<
TAssociateRoleAssignmentDraft,
TAssociateRoleAssignmentDraftGraphql
>(
'graphql',
AssociateRoleAssignmentDraft.random(),
expect.objectContaining({
associateRole: expect.objectContaining({
typeId: 'associate-role',
id: expect.any(String),
}),
inheritance: expect.any(String),
})
)
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Builder } from '@commercetools-test-data/core';
import generator from './generator';
import transformers from './transformers';
import type {
TCreateAssociateRoleAssignmentDraftBuilder,
TAssociateRoleAssignmentDraft,
} from '../types';

const Model: TCreateAssociateRoleAssignmentDraftBuilder = () =>
Builder<TAssociateRoleAssignmentDraft>({
generator,
transformers,
});

export default Model;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ReferenceDraft } from '@commercetools-test-data/commons';
import { fake, Generator, oneOf } from '@commercetools-test-data/core';
import { associateRoleInheritanceMode } from '../constants';
import { TAssociateRoleAssignmentDraft } from '../types';

// https://docs.commercetools.com/api/projects/business-units#associateroleassignmentdraft

const generator = Generator<TAssociateRoleAssignmentDraft>({
fields: {
associateRole: fake(() =>
ReferenceDraft.presets.associateRoleReference.associateRoleReference()
),
inheritance: oneOf(...Object.values(associateRoleInheritanceMode)),
},
});

export default generator;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as random } from './builder';
export { default as presets } from './presets';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const presets = {};

export default presets;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Transformer } from '@commercetools-test-data/core';
import type {
TAssociateRoleAssignmentDraft,
TAssociateRoleAssignmentDraftGraphql,
} from '../types';

const transformers = {
default: Transformer<
TAssociateRoleAssignmentDraft,
TAssociateRoleAssignmentDraft
>('default', { buildFields: ['associateRole'] }),
rest: Transformer<
TAssociateRoleAssignmentDraft,
TAssociateRoleAssignmentDraft
>('rest', { buildFields: ['associateRole'] }),
graphql: Transformer<
TAssociateRoleAssignmentDraft,
TAssociateRoleAssignmentDraftGraphql
>('graphql', { buildFields: ['associateRole'] }),
};

export default transformers;
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* eslint-disable jest/no-disabled-tests */
/* eslint-disable jest/valid-title */
import { createBuilderSpec } from '@commercetools-test-data/core/test-utils';
import {
TAssociateRoleAssignmentDefault,
TAssociateRoleAssignmentRest,
TAssociateRoleAssignmentGraphql,
} from './types';
import * as AssociateRoleAssignment from '.';

describe('builder', () => {
it(
...createBuilderSpec<
TAssociateRoleAssignmentDefault,
TAssociateRoleAssignmentDefault
>(
'default',
AssociateRoleAssignment.random(),
expect.objectContaining({
associateRole: expect.objectContaining({
id: expect.any(String),
}),
inheritance: expect.any(String),
})
)
);

it(
...createBuilderSpec<
TAssociateRoleAssignmentDefault,
TAssociateRoleAssignmentRest
>(
'rest',
AssociateRoleAssignment.random(),
expect.objectContaining({
associateRole: expect.objectContaining({
typeId: 'associate-role',
}),
inheritance: expect.any(String),
})
)
);

it(
...createBuilderSpec<
TAssociateRoleAssignmentDefault,
TAssociateRoleAssignmentGraphql
>(
'graphql',
AssociateRoleAssignment.random(),
expect.objectContaining({
associateRole: expect.objectContaining({
id: expect.any(String),
}),
associateRoleRef: expect.objectContaining({
typeId: 'associate-role',
key: expect.any(String),
__typename: 'Reference',
}),
inheritance: expect.any(String),
__typename: 'AssociateRoleAssignment',
})
)
);
});
15 changes: 15 additions & 0 deletions models/business-unit/src/associate-role-assignment/builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Builder } from '@commercetools-test-data/core';
import generator from './generator';
import transformers from './transformers';
import type {
TAssociateRoleAssignmentDefault,
TCreateAssociateRoleAssignmentBuilder,
} from './types';

const Model: TCreateAssociateRoleAssignmentBuilder = () =>
Builder<TAssociateRoleAssignmentDefault>({
generator,
transformers,
});

export default Model;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const associateRoleInheritanceMode = {
Enabled: 'Enabled',
Disabled: 'Disabled',
} as const;
15 changes: 15 additions & 0 deletions models/business-unit/src/associate-role-assignment/generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { AssociateRole } from '@commercetools-test-data/associate-role';
import { fake, Generator, oneOf } from '@commercetools-test-data/core';
import { associateRoleInheritanceMode } from './constants';
import { TAssociateRoleAssignmentDefault } from './types';

// https://docs.commercetools.com/api/projects/business-units#associateroleassignment

const generator = Generator<TAssociateRoleAssignmentDefault>({
fields: {
associateRole: fake(() => AssociateRole.random()),
inheritance: oneOf(...Object.values(associateRoleInheritanceMode)),
},
});

export default generator;
5 changes: 5 additions & 0 deletions models/business-unit/src/associate-role-assignment/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './types';
export * as AssociateRoleAssignmentDraft from './associate-role-assignment-draft';

export { default as random } from './builder';
export { default as presets } from './presets';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const presets = {};

export default presets;
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { type AssociateRoleKeyReference } from '@commercetools/platform-sdk';
import { KeyReference } from '@commercetools-test-data/commons';
import { Transformer } from '@commercetools-test-data/core';
import type {
TAssociateRoleAssignmentDefault,
TAssociateRoleAssignmentRest,
TAssociateRoleAssignmentGraphql,
} from './types';

const transformers = {
default: Transformer<
TAssociateRoleAssignmentDefault,
TAssociateRoleAssignmentDefault
>('default', {
buildFields: ['associateRole'],
}),
rest: Transformer<
TAssociateRoleAssignmentDefault,
TAssociateRoleAssignmentRest
>('rest', {
buildFields: ['associateRole'],
replaceFields: ({ fields }) => {
const associateRole: AssociateRoleKeyReference = KeyReference.presets
.associateRole()
.key(fields.associateRole.key)
.build();
stephsprinkle marked this conversation as resolved.
Show resolved Hide resolved
return {
...fields,
associateRole,
};
},
}),
graphql: Transformer<
TAssociateRoleAssignmentDefault,
TAssociateRoleAssignmentGraphql
>('graphql', {
buildFields: ['associateRole'],
addFields: ({ fields }) => {
const associateRoleRef: AssociateRoleKeyReference = KeyReference.presets
.associateRole()
.key(fields.associateRole.key)
.buildGraphql();
stephsprinkle marked this conversation as resolved.
Show resolved Hide resolved

return {
...fields,
associateRoleRef,
__typename: 'AssociateRoleAssignment',
};
},
}),
};

export default transformers;
33 changes: 33 additions & 0 deletions models/business-unit/src/associate-role-assignment/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type {
AssociateRoleKeyReference,
AssociateRole,
AssociateRoleAssignment,
AssociateRoleAssignmentDraft,
} from '@commercetools/platform-sdk';
import type { TBuilder } from '@commercetools-test-data/core';

export type TAssociateRoleAssignmentDraft = AssociateRoleAssignmentDraft;
export type TAssociateRoleAssignmentDraftGraphql =
TAssociateRoleAssignmentDraft;

export type TAssociateRoleAssignmentDefault = Omit<
AssociateRoleAssignment,
'associateRole'
> & {
associateRole: AssociateRole;
};
export type TAssociateRoleAssignmentRest = AssociateRoleAssignment;
export type TAssociateRoleAssignmentGraphql =
TAssociateRoleAssignmentDefault & {
associateRoleRef: AssociateRoleKeyReference | null;
__typename: 'AssociateRoleAssignment';
};

export type TAssociateRoleAssignmentDraftBuilder =
TBuilder<TAssociateRoleAssignmentDraft>;
export type TCreateAssociateRoleAssignmentDraftBuilder =
() => TAssociateRoleAssignmentDraftBuilder;
export type TAssociateRoleAssignmentBuilder =
TBuilder<TAssociateRoleAssignmentDefault>;
export type TCreateAssociateRoleAssignmentBuilder =
() => TAssociateRoleAssignmentBuilder;
Loading
Loading