From 9291fd74f07a4aa9bed28ef88c5f68f90a1d0e4b Mon Sep 17 00:00:00 2001 From: AlexKMarshall Date: Fri, 30 Sep 2022 16:28:32 +0100 Subject: [PATCH 1/9] refactor: extract out public types --- .../src/faction/create/createFaction.test.ts | 7 +-- .../src/faction/create/createFaction.ts | 46 ++----------------- .../src/faction/create/implementation.test.ts | 2 +- .../src/faction/create/implementation.ts | 26 +++++------ apps/domain/src/faction/create/index.ts | 1 + apps/domain/src/faction/create/types.ts | 41 +++++++++++++++++ apps/domain/src/gang/foundGang/index.ts | 1 + 7 files changed, 65 insertions(+), 59 deletions(-) create mode 100644 apps/domain/src/faction/create/types.ts diff --git a/apps/domain/src/faction/create/createFaction.test.ts b/apps/domain/src/faction/create/createFaction.test.ts index 046b5db..47b6fc5 100644 --- a/apps/domain/src/faction/create/createFaction.test.ts +++ b/apps/domain/src/faction/create/createFaction.test.ts @@ -1,15 +1,16 @@ import { describe, it, expect } from 'vitest' -import { createFaction, UnvalidatedFaction } from './createFaction' +import { createFaction } from './createFaction' +import { UnvalidatedFaction } from './types' describe('createFaction', () => { it('should return a list of events', () => { - const checkFactionExists = () => false + const checkFactionNameExists = () => false const unvalidatedFaction: UnvalidatedFaction = { name: 'Van Saar', } expect( - createFaction({ checkFactionExists })(unvalidatedFaction) + createFaction({ checkFactionNameExists })(unvalidatedFaction) ).toStrictEqualRight([ { event: 'factionCreated', diff --git a/apps/domain/src/faction/create/createFaction.ts b/apps/domain/src/faction/create/createFaction.ts index 04a78c3..a7c0531 100644 --- a/apps/domain/src/faction/create/createFaction.ts +++ b/apps/domain/src/faction/create/createFaction.ts @@ -1,52 +1,14 @@ import { pipe } from 'fp-ts/lib/function' -import { Opaque } from 'type-fest' -import { FactionId } from './factionId' -import { - createEvents, - FactionValidationError, - UniqueFactionName, - validateFaction, -} from './implementation' +import { createEvents, validateFaction } from './implementation' import * as E from 'fp-ts/Either' - -export type UnvalidatedFaction = { - name: string -} - -export type FactionName = Opaque - -export type ValidatedFaction = { - name: UniqueFactionName - id: FactionId -} - -export type FactionCreated = { - event: 'factionCreated' - details: ValidatedFaction -} - -export type CreateFactionEvent = FactionCreated - -export type CheckFactionExists = (name: string) => boolean - -export type CreateFactionDependencies = { - checkFactionExists: CheckFactionExists -} - -export type CreateFactionError = FactionValidationError - -export type CreateFaction = ( - dependencies: CreateFactionDependencies -) => ( - unvalidatedFaction: UnvalidatedFaction -) => E.Either +import { CreateFaction } from './types' export const createFaction: CreateFaction = - ({ checkFactionExists }) => + ({ checkFactionNameExists }) => (unvalidatedFaction) => { return pipe( unvalidatedFaction, - validateFaction(checkFactionExists), + validateFaction(checkFactionNameExists), E.map(createEvents) ) } diff --git a/apps/domain/src/faction/create/implementation.test.ts b/apps/domain/src/faction/create/implementation.test.ts index fbb16f4..87bfd3f 100644 --- a/apps/domain/src/faction/create/implementation.test.ts +++ b/apps/domain/src/faction/create/implementation.test.ts @@ -1,6 +1,6 @@ import { describe, it, expect } from 'vitest' -import { UnvalidatedFaction } from './createFaction' import { toValidFactionName, validateFaction } from './implementation' +import { UnvalidatedFaction } from './types' describe('toValidFactionName', () => { it('should pass a name that does not exist already', () => { diff --git a/apps/domain/src/faction/create/implementation.ts b/apps/domain/src/faction/create/implementation.ts index 201e8b0..f4c9afc 100644 --- a/apps/domain/src/faction/create/implementation.ts +++ b/apps/domain/src/faction/create/implementation.ts @@ -1,29 +1,27 @@ import { sequenceS } from 'fp-ts/lib/Apply' import { flow, pipe } from 'fp-ts/lib/function' import * as E from 'fp-ts/Either' -import * as String50 from '../../common/string50' -import { - CreateFactionEvent, - UnvalidatedFaction, - ValidatedFaction, -} from './createFaction' import * as FactionId from './factionId' import { ConstrainedStringError } from '../../common/constrained' import { Opaque } from 'type-fest' import * as FactionName from './name' +import { + UnvalidatedFaction, + ValidatedFaction, + CreateFactionEvent, + CheckFactionNameExists, +} from './types' export type FactionValidationError = | ConstrainedStringError | FactionNameAlreadyExistsError type ValidateFaction = ( - checkFactionExists: CheckFactionExists + checkFactionNameExists: CheckFactionNameExists ) => ( unvalidatedFaction: UnvalidatedFaction ) => E.Either -type CheckFactionExists = (name: string) => boolean - class FactionNameAlreadyExistsError extends Error { public _tag: 'FactionNameAlreadyExistsError' private constructor(name: string) { @@ -41,20 +39,22 @@ const _tag = (name: FactionName.FactionName): UniqueFactionName => export type UniqueFactionName = Opaque const toUniqueFactionName = - (checkFactionExists: CheckFactionExists) => + (checkFactionNameExists: CheckFactionNameExists) => ( name: FactionName.FactionName ): E.Either => { - if (checkFactionExists(name)) { + if (checkFactionNameExists(name)) { return pipe(name, FactionNameAlreadyExistsError.of, E.left) } return pipe(name, _tag, E.right) } -export const toValidFactionName = (checkFactionExists: CheckFactionExists) => +export const toValidFactionName = ( + checkFactionNameExists: CheckFactionNameExists +) => flow( FactionName.parse('name'), - E.chainW(toUniqueFactionName(checkFactionExists)) + E.chainW(toUniqueFactionName(checkFactionNameExists)) ) export const validateFaction: ValidateFaction = diff --git a/apps/domain/src/faction/create/index.ts b/apps/domain/src/faction/create/index.ts index 570d92e..c6b7062 100644 --- a/apps/domain/src/faction/create/index.ts +++ b/apps/domain/src/faction/create/index.ts @@ -1 +1,2 @@ export * from './createFaction' +export * from './types' diff --git a/apps/domain/src/faction/create/types.ts b/apps/domain/src/faction/create/types.ts new file mode 100644 index 0000000..83d97df --- /dev/null +++ b/apps/domain/src/faction/create/types.ts @@ -0,0 +1,41 @@ +import { FactionId } from './factionId' +import { FactionValidationError, UniqueFactionName } from './implementation' +import * as E from 'fp-ts/Either' + +/* + * External dependencies / DB Functions + */ + +export type CheckFactionNameExists = (name: string) => boolean + +/* + * Public API + */ + +export type UnvalidatedFaction = { + name: string +} + +export type ValidatedFaction = { + name: UniqueFactionName + id: FactionId +} + +export type FactionCreated = { + event: 'factionCreated' + details: ValidatedFaction +} + +export type CreateFactionEvent = FactionCreated + +export type CreateFactionError = FactionValidationError + +export type CreateFactionDependencies = { + checkFactionNameExists: CheckFactionNameExists +} + +export type CreateFaction = ( + dependencies: CreateFactionDependencies +) => ( + unvalidatedFaction: UnvalidatedFaction +) => E.Either diff --git a/apps/domain/src/gang/foundGang/index.ts b/apps/domain/src/gang/foundGang/index.ts index 3c77285..af48a4b 100644 --- a/apps/domain/src/gang/foundGang/index.ts +++ b/apps/domain/src/gang/foundGang/index.ts @@ -1 +1,2 @@ export * from './foundGang' +export * from './types' From 2914707a10bb970ec75d3bfa8de597056b704a35 Mon Sep 17 00:00:00 2001 From: AlexKMarshall Date: Fri, 30 Sep 2022 17:18:47 +0100 Subject: [PATCH 2/9] refactor: create new Task based types --- apps/domain/src/faction/create/types.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/apps/domain/src/faction/create/types.ts b/apps/domain/src/faction/create/types.ts index 83d97df..f92393b 100644 --- a/apps/domain/src/faction/create/types.ts +++ b/apps/domain/src/faction/create/types.ts @@ -1,12 +1,15 @@ import { FactionId } from './factionId' import { FactionValidationError, UniqueFactionName } from './implementation' import * as E from 'fp-ts/Either' +import * as T from 'fp-ts/Task' +import * as TE from 'fp-ts/TaskEither' /* * External dependencies / DB Functions */ export type CheckFactionNameExists = (name: string) => boolean +export type CheckFactionNameExistsT = (name: string) => T.Task /* * Public API @@ -33,9 +36,18 @@ export type CreateFactionError = FactionValidationError export type CreateFactionDependencies = { checkFactionNameExists: CheckFactionNameExists } +export type CreateFactionDependenciesT = { + checkFactionNameExists: CheckFactionNameExistsT +} export type CreateFaction = ( dependencies: CreateFactionDependencies ) => ( unvalidatedFaction: UnvalidatedFaction ) => E.Either + +export type CreateFactionT = ( + dependencies: CreateFactionDependenciesT +) => ( + unvalidatedFaction: UnvalidatedFaction +) => TE.TaskEither From 02807903e3c8744b531b2567f5037983a7dfb370 Mon Sep 17 00:00:00 2001 From: AlexKMarshall Date: Fri, 30 Sep 2022 18:00:38 +0100 Subject: [PATCH 3/9] refactor: create new task based faction name validator --- .../src/faction/create/implementation.test.ts | 23 ++++++++++++- .../src/faction/create/implementation.ts | 34 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/apps/domain/src/faction/create/implementation.test.ts b/apps/domain/src/faction/create/implementation.test.ts index 87bfd3f..5583e76 100644 --- a/apps/domain/src/faction/create/implementation.test.ts +++ b/apps/domain/src/faction/create/implementation.test.ts @@ -1,6 +1,11 @@ import { describe, it, expect } from 'vitest' -import { toValidFactionName, validateFaction } from './implementation' +import { + toValidFactionName, + toValidFactionNameT, + validateFaction, +} from './implementation' import { UnvalidatedFaction } from './types' +import * as T from 'fp-ts/Task' describe('toValidFactionName', () => { it('should pass a name that does not exist already', () => { @@ -17,6 +22,22 @@ describe('toValidFactionName', () => { }) }) +describe('toValidFactionNameT', () => { + it('should pass a name that does not exist already', async () => { + const checkFactionExists = () => T.of(false) + const factionName = 'Goliath' + const actual = await toValidFactionNameT(checkFactionExists)(factionName)() + expect(actual).toStrictEqualRight(factionName) + }) + it('should reject pre-existing faction', async () => { + const checkFactionExists = () => T.of(true) + const factionName = 'Escher' + expect( + await toValidFactionNameT(checkFactionExists)(factionName)() + ).toBeLeft() + }) +}) + describe('validateFaction', () => { it('should pass a valid faction', () => { const unvalidatedFaction: UnvalidatedFaction = { diff --git a/apps/domain/src/faction/create/implementation.ts b/apps/domain/src/faction/create/implementation.ts index f4c9afc..fa9bc01 100644 --- a/apps/domain/src/faction/create/implementation.ts +++ b/apps/domain/src/faction/create/implementation.ts @@ -10,7 +10,10 @@ import { ValidatedFaction, CreateFactionEvent, CheckFactionNameExists, + CheckFactionNameExistsT, } from './types' +import * as TE from 'fp-ts/TaskEither' +import * as T from 'fp-ts/Task' export type FactionValidationError = | ConstrainedStringError @@ -22,6 +25,12 @@ type ValidateFaction = ( unvalidatedFaction: UnvalidatedFaction ) => E.Either +type ValidateFactionT = ( + checkFactionNameExists: CheckFactionNameExistsT +) => ( + unvalidatedFaction: UnvalidatedFaction +) => TE.TaskEither + class FactionNameAlreadyExistsError extends Error { public _tag: 'FactionNameAlreadyExistsError' private constructor(name: string) { @@ -49,6 +58,22 @@ const toUniqueFactionName = return pipe(name, _tag, E.right) } +const toUniqueFactionNameT = + (checkFactionNameExists: CheckFactionNameExistsT) => + ( + name: FactionName.FactionName + ): TE.TaskEither => { + return pipe( + name, + checkFactionNameExists, + T.map((exists) => + exists + ? pipe(name, FactionNameAlreadyExistsError.of, E.left) + : pipe(name, _tag, E.right) + ) + ) + } + export const toValidFactionName = ( checkFactionNameExists: CheckFactionNameExists ) => @@ -57,6 +82,15 @@ export const toValidFactionName = ( E.chainW(toUniqueFactionName(checkFactionNameExists)) ) +export const toValidFactionNameT = ( + checkFactionNameExists: CheckFactionNameExistsT +) => + flow( + FactionName.parse('name'), + TE.fromEither, + TE.chainW(toUniqueFactionNameT(checkFactionNameExists)) + ) + export const validateFaction: ValidateFaction = (checkFactionExists) => (unvalidatedFaction) => { return pipe(unvalidatedFaction, ({ name }) => From b13bee628333b860ad107304ac41f8c711eb929a Mon Sep 17 00:00:00 2001 From: AlexKMarshall Date: Fri, 30 Sep 2022 18:06:00 +0100 Subject: [PATCH 4/9] refactor: create new task based faction validator --- .../src/faction/create/implementation.test.ts | 16 ++++++++++++++++ apps/domain/src/faction/create/implementation.ts | 10 ++++++++++ 2 files changed, 26 insertions(+) diff --git a/apps/domain/src/faction/create/implementation.test.ts b/apps/domain/src/faction/create/implementation.test.ts index 5583e76..b6a6541 100644 --- a/apps/domain/src/faction/create/implementation.test.ts +++ b/apps/domain/src/faction/create/implementation.test.ts @@ -3,6 +3,7 @@ import { toValidFactionName, toValidFactionNameT, validateFaction, + validateFactionT, } from './implementation' import { UnvalidatedFaction } from './types' import * as T from 'fp-ts/Task' @@ -52,3 +53,18 @@ describe('validateFaction', () => { }) }) }) + +describe('validateFactionT', () => { + it('should pass a valid faction', async () => { + const unvalidatedFaction: UnvalidatedFaction = { + name: 'Orlock', + } + const checkFactionExists = () => T.of(false) + expect( + await validateFactionT(checkFactionExists)(unvalidatedFaction)() + ).toStrictEqualRight({ + id: expect.any(String), + name: unvalidatedFaction.name, + }) + }) +}) diff --git a/apps/domain/src/faction/create/implementation.ts b/apps/domain/src/faction/create/implementation.ts index fa9bc01..660bf24 100644 --- a/apps/domain/src/faction/create/implementation.ts +++ b/apps/domain/src/faction/create/implementation.ts @@ -101,6 +101,16 @@ export const validateFaction: ValidateFaction = ) } +export const validateFactionT: ValidateFactionT = + (checkFactionExists) => (unvalidatedFaction) => { + return pipe(unvalidatedFaction, ({ name }) => + sequenceS(TE.ApplySeq)({ + id: pipe(FactionId.create(), TE.right), + name: pipe(name, toValidFactionNameT(checkFactionExists)), + }) + ) + } + type CreateEvents = (validatedFaction: ValidatedFaction) => CreateFactionEvent[] export const createEvents: CreateEvents = (validatedFaction) => { From d6e8a37406eb63d451f8de7bdf3c931dab484741 Mon Sep 17 00:00:00 2001 From: AlexKMarshall Date: Fri, 30 Sep 2022 18:09:25 +0100 Subject: [PATCH 5/9] refactor: create new task based createFaction workflow --- .../src/faction/create/createFaction.test.ts | 21 ++++++++++++++++++- .../src/faction/create/createFaction.ts | 19 +++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/apps/domain/src/faction/create/createFaction.test.ts b/apps/domain/src/faction/create/createFaction.test.ts index 47b6fc5..5d79f53 100644 --- a/apps/domain/src/faction/create/createFaction.test.ts +++ b/apps/domain/src/faction/create/createFaction.test.ts @@ -1,5 +1,6 @@ +import * as T from 'fp-ts/Task' import { describe, it, expect } from 'vitest' -import { createFaction } from './createFaction' +import { createFaction, createFactionT } from './createFaction' import { UnvalidatedFaction } from './types' describe('createFaction', () => { @@ -19,3 +20,21 @@ describe('createFaction', () => { ]) }) }) + +describe('createFactionT', () => { + it('should return a list of events', async () => { + const checkFactionNameExists = () => T.of(false) + const unvalidatedFaction: UnvalidatedFaction = { + name: 'Van Saar', + } + + expect( + await createFactionT({ checkFactionNameExists })(unvalidatedFaction)() + ).toStrictEqualRight([ + { + event: 'factionCreated', + details: { id: expect.any(String), name: unvalidatedFaction.name }, + }, + ]) + }) +}) diff --git a/apps/domain/src/faction/create/createFaction.ts b/apps/domain/src/faction/create/createFaction.ts index a7c0531..c0263aa 100644 --- a/apps/domain/src/faction/create/createFaction.ts +++ b/apps/domain/src/faction/create/createFaction.ts @@ -1,7 +1,12 @@ import { pipe } from 'fp-ts/lib/function' -import { createEvents, validateFaction } from './implementation' +import { + createEvents, + validateFaction, + validateFactionT, +} from './implementation' import * as E from 'fp-ts/Either' -import { CreateFaction } from './types' +import * as TE from 'fp-ts/TaskEither' +import { CreateFaction, CreateFactionT } from './types' export const createFaction: CreateFaction = ({ checkFactionNameExists }) => @@ -12,3 +17,13 @@ export const createFaction: CreateFaction = E.map(createEvents) ) } + +export const createFactionT: CreateFactionT = + ({ checkFactionNameExists }) => + (unvalidatedFaction) => { + return pipe( + unvalidatedFaction, + validateFactionT(checkFactionNameExists), + TE.map(createEvents) + ) + } From b672e3bb18697fde48342642d3861de8a8471d47 Mon Sep 17 00:00:00 2001 From: AlexKMarshall Date: Fri, 30 Sep 2022 18:10:51 +0100 Subject: [PATCH 6/9] refactor: replace createFaction with task based --- .../src/faction/create/createFaction.test.ts | 22 ++----------------- .../src/faction/create/createFaction.ts | 21 +++--------------- 2 files changed, 5 insertions(+), 38 deletions(-) diff --git a/apps/domain/src/faction/create/createFaction.test.ts b/apps/domain/src/faction/create/createFaction.test.ts index 5d79f53..fc64cf8 100644 --- a/apps/domain/src/faction/create/createFaction.test.ts +++ b/apps/domain/src/faction/create/createFaction.test.ts @@ -1,27 +1,9 @@ import * as T from 'fp-ts/Task' import { describe, it, expect } from 'vitest' -import { createFaction, createFactionT } from './createFaction' +import { createFaction } from './createFaction' import { UnvalidatedFaction } from './types' describe('createFaction', () => { - it('should return a list of events', () => { - const checkFactionNameExists = () => false - const unvalidatedFaction: UnvalidatedFaction = { - name: 'Van Saar', - } - - expect( - createFaction({ checkFactionNameExists })(unvalidatedFaction) - ).toStrictEqualRight([ - { - event: 'factionCreated', - details: { id: expect.any(String), name: unvalidatedFaction.name }, - }, - ]) - }) -}) - -describe('createFactionT', () => { it('should return a list of events', async () => { const checkFactionNameExists = () => T.of(false) const unvalidatedFaction: UnvalidatedFaction = { @@ -29,7 +11,7 @@ describe('createFactionT', () => { } expect( - await createFactionT({ checkFactionNameExists })(unvalidatedFaction)() + await createFaction({ checkFactionNameExists })(unvalidatedFaction)() ).toStrictEqualRight([ { event: 'factionCreated', diff --git a/apps/domain/src/faction/create/createFaction.ts b/apps/domain/src/faction/create/createFaction.ts index c0263aa..6547424 100644 --- a/apps/domain/src/faction/create/createFaction.ts +++ b/apps/domain/src/faction/create/createFaction.ts @@ -1,24 +1,9 @@ import { pipe } from 'fp-ts/lib/function' -import { - createEvents, - validateFaction, - validateFactionT, -} from './implementation' -import * as E from 'fp-ts/Either' +import { createEvents, validateFactionT } from './implementation' import * as TE from 'fp-ts/TaskEither' -import { CreateFaction, CreateFactionT } from './types' +import { CreateFactionT } from './types' -export const createFaction: CreateFaction = - ({ checkFactionNameExists }) => - (unvalidatedFaction) => { - return pipe( - unvalidatedFaction, - validateFaction(checkFactionNameExists), - E.map(createEvents) - ) - } - -export const createFactionT: CreateFactionT = +export const createFaction: CreateFactionT = ({ checkFactionNameExists }) => (unvalidatedFaction) => { return pipe( From c05900ad6e7a4170a7b24eaade6a8e31e6c3b27a Mon Sep 17 00:00:00 2001 From: AlexKMarshall Date: Fri, 30 Sep 2022 18:11:55 +0100 Subject: [PATCH 7/9] refactor: replace validateFaction with task based --- .../domain/src/faction/create/createFaction.ts | 4 ++-- .../src/faction/create/implementation.test.ts | 18 +----------------- .../src/faction/create/implementation.ts | 12 +----------- 3 files changed, 4 insertions(+), 30 deletions(-) diff --git a/apps/domain/src/faction/create/createFaction.ts b/apps/domain/src/faction/create/createFaction.ts index 6547424..8f44b7c 100644 --- a/apps/domain/src/faction/create/createFaction.ts +++ b/apps/domain/src/faction/create/createFaction.ts @@ -1,5 +1,5 @@ import { pipe } from 'fp-ts/lib/function' -import { createEvents, validateFactionT } from './implementation' +import { createEvents, validateFaction } from './implementation' import * as TE from 'fp-ts/TaskEither' import { CreateFactionT } from './types' @@ -8,7 +8,7 @@ export const createFaction: CreateFactionT = (unvalidatedFaction) => { return pipe( unvalidatedFaction, - validateFactionT(checkFactionNameExists), + validateFaction(checkFactionNameExists), TE.map(createEvents) ) } diff --git a/apps/domain/src/faction/create/implementation.test.ts b/apps/domain/src/faction/create/implementation.test.ts index b6a6541..8d9489e 100644 --- a/apps/domain/src/faction/create/implementation.test.ts +++ b/apps/domain/src/faction/create/implementation.test.ts @@ -3,7 +3,6 @@ import { toValidFactionName, toValidFactionNameT, validateFaction, - validateFactionT, } from './implementation' import { UnvalidatedFaction } from './types' import * as T from 'fp-ts/Task' @@ -40,28 +39,13 @@ describe('toValidFactionNameT', () => { }) describe('validateFaction', () => { - it('should pass a valid faction', () => { - const unvalidatedFaction: UnvalidatedFaction = { - name: 'Orlock', - } - const checkFactionExists = () => false - expect( - validateFaction(checkFactionExists)(unvalidatedFaction) - ).toStrictEqualRight({ - id: expect.any(String), - name: unvalidatedFaction.name, - }) - }) -}) - -describe('validateFactionT', () => { it('should pass a valid faction', async () => { const unvalidatedFaction: UnvalidatedFaction = { name: 'Orlock', } const checkFactionExists = () => T.of(false) expect( - await validateFactionT(checkFactionExists)(unvalidatedFaction)() + await validateFaction(checkFactionExists)(unvalidatedFaction)() ).toStrictEqualRight({ id: expect.any(String), name: unvalidatedFaction.name, diff --git a/apps/domain/src/faction/create/implementation.ts b/apps/domain/src/faction/create/implementation.ts index 660bf24..5f1fe06 100644 --- a/apps/domain/src/faction/create/implementation.ts +++ b/apps/domain/src/faction/create/implementation.ts @@ -91,17 +91,7 @@ export const toValidFactionNameT = ( TE.chainW(toUniqueFactionNameT(checkFactionNameExists)) ) -export const validateFaction: ValidateFaction = - (checkFactionExists) => (unvalidatedFaction) => { - return pipe(unvalidatedFaction, ({ name }) => - sequenceS(E.Apply)({ - id: pipe(FactionId.create(), E.right), - name: pipe(name, toValidFactionName(checkFactionExists)), - }) - ) - } - -export const validateFactionT: ValidateFactionT = +export const validateFaction: ValidateFactionT = (checkFactionExists) => (unvalidatedFaction) => { return pipe(unvalidatedFaction, ({ name }) => sequenceS(TE.ApplySeq)({ From 2de0e14943268433c0194e6b85056d759d0279d9 Mon Sep 17 00:00:00 2001 From: AlexKMarshall Date: Fri, 30 Sep 2022 18:13:13 +0100 Subject: [PATCH 8/9] refactor: replace toValidFactionName with task based --- .../src/faction/create/implementation.test.ts | 21 +----------------- .../src/faction/create/implementation.ts | 22 ++----------------- 2 files changed, 3 insertions(+), 40 deletions(-) diff --git a/apps/domain/src/faction/create/implementation.test.ts b/apps/domain/src/faction/create/implementation.test.ts index 8d9489e..a6b44fe 100644 --- a/apps/domain/src/faction/create/implementation.test.ts +++ b/apps/domain/src/faction/create/implementation.test.ts @@ -1,28 +1,9 @@ import { describe, it, expect } from 'vitest' -import { - toValidFactionName, - toValidFactionNameT, - validateFaction, -} from './implementation' +import { toValidFactionNameT, validateFaction } from './implementation' import { UnvalidatedFaction } from './types' import * as T from 'fp-ts/Task' describe('toValidFactionName', () => { - it('should pass a name that does not exist already', () => { - const checkFactionExists = () => false - const factionName = 'Goliath' - expect( - toValidFactionName(checkFactionExists)(factionName) - ).toStrictEqualRight(factionName) - }) - it('should reject pre-existing faction', () => { - const checkFactionExists = () => true - const factionName = 'Escher' - expect(toValidFactionName(checkFactionExists)(factionName)).toBeLeft() - }) -}) - -describe('toValidFactionNameT', () => { it('should pass a name that does not exist already', async () => { const checkFactionExists = () => T.of(false) const factionName = 'Goliath' diff --git a/apps/domain/src/faction/create/implementation.ts b/apps/domain/src/faction/create/implementation.ts index 5f1fe06..7fb2cf6 100644 --- a/apps/domain/src/faction/create/implementation.ts +++ b/apps/domain/src/faction/create/implementation.ts @@ -47,18 +47,8 @@ const _tag = (name: FactionName.FactionName): UniqueFactionName => FactionName.value(name) as UniqueFactionName export type UniqueFactionName = Opaque -const toUniqueFactionName = - (checkFactionNameExists: CheckFactionNameExists) => - ( - name: FactionName.FactionName - ): E.Either => { - if (checkFactionNameExists(name)) { - return pipe(name, FactionNameAlreadyExistsError.of, E.left) - } - return pipe(name, _tag, E.right) - } -const toUniqueFactionNameT = +const toUniqueFactionName = (checkFactionNameExists: CheckFactionNameExistsT) => ( name: FactionName.FactionName @@ -74,21 +64,13 @@ const toUniqueFactionNameT = ) } -export const toValidFactionName = ( - checkFactionNameExists: CheckFactionNameExists -) => - flow( - FactionName.parse('name'), - E.chainW(toUniqueFactionName(checkFactionNameExists)) - ) - export const toValidFactionNameT = ( checkFactionNameExists: CheckFactionNameExistsT ) => flow( FactionName.parse('name'), TE.fromEither, - TE.chainW(toUniqueFactionNameT(checkFactionNameExists)) + TE.chainW(toUniqueFactionName(checkFactionNameExists)) ) export const validateFaction: ValidateFactionT = From 8d0436612922cb8d37f7008496850e4ca0d99ffe Mon Sep 17 00:00:00 2001 From: AlexKMarshall Date: Fri, 30 Sep 2022 18:14:55 +0100 Subject: [PATCH 9/9] refactor: replace types with task based versions --- apps/domain/src/faction/create/createFaction.ts | 4 ++-- apps/domain/src/faction/create/implementation.ts | 13 +++---------- apps/domain/src/faction/create/types.ts | 12 +----------- 3 files changed, 6 insertions(+), 23 deletions(-) diff --git a/apps/domain/src/faction/create/createFaction.ts b/apps/domain/src/faction/create/createFaction.ts index 8f44b7c..d7fb237 100644 --- a/apps/domain/src/faction/create/createFaction.ts +++ b/apps/domain/src/faction/create/createFaction.ts @@ -1,9 +1,9 @@ import { pipe } from 'fp-ts/lib/function' import { createEvents, validateFaction } from './implementation' import * as TE from 'fp-ts/TaskEither' -import { CreateFactionT } from './types' +import { CreateFaction } from './types' -export const createFaction: CreateFactionT = +export const createFaction: CreateFaction = ({ checkFactionNameExists }) => (unvalidatedFaction) => { return pipe( diff --git a/apps/domain/src/faction/create/implementation.ts b/apps/domain/src/faction/create/implementation.ts index 7fb2cf6..b310ba7 100644 --- a/apps/domain/src/faction/create/implementation.ts +++ b/apps/domain/src/faction/create/implementation.ts @@ -10,7 +10,6 @@ import { ValidatedFaction, CreateFactionEvent, CheckFactionNameExists, - CheckFactionNameExistsT, } from './types' import * as TE from 'fp-ts/TaskEither' import * as T from 'fp-ts/Task' @@ -23,12 +22,6 @@ type ValidateFaction = ( checkFactionNameExists: CheckFactionNameExists ) => ( unvalidatedFaction: UnvalidatedFaction -) => E.Either - -type ValidateFactionT = ( - checkFactionNameExists: CheckFactionNameExistsT -) => ( - unvalidatedFaction: UnvalidatedFaction ) => TE.TaskEither class FactionNameAlreadyExistsError extends Error { @@ -49,7 +42,7 @@ const _tag = (name: FactionName.FactionName): UniqueFactionName => export type UniqueFactionName = Opaque const toUniqueFactionName = - (checkFactionNameExists: CheckFactionNameExistsT) => + (checkFactionNameExists: CheckFactionNameExists) => ( name: FactionName.FactionName ): TE.TaskEither => { @@ -65,7 +58,7 @@ const toUniqueFactionName = } export const toValidFactionNameT = ( - checkFactionNameExists: CheckFactionNameExistsT + checkFactionNameExists: CheckFactionNameExists ) => flow( FactionName.parse('name'), @@ -73,7 +66,7 @@ export const toValidFactionNameT = ( TE.chainW(toUniqueFactionName(checkFactionNameExists)) ) -export const validateFaction: ValidateFactionT = +export const validateFaction: ValidateFaction = (checkFactionExists) => (unvalidatedFaction) => { return pipe(unvalidatedFaction, ({ name }) => sequenceS(TE.ApplySeq)({ diff --git a/apps/domain/src/faction/create/types.ts b/apps/domain/src/faction/create/types.ts index f92393b..a793fb9 100644 --- a/apps/domain/src/faction/create/types.ts +++ b/apps/domain/src/faction/create/types.ts @@ -8,8 +8,7 @@ import * as TE from 'fp-ts/TaskEither' * External dependencies / DB Functions */ -export type CheckFactionNameExists = (name: string) => boolean -export type CheckFactionNameExistsT = (name: string) => T.Task +export type CheckFactionNameExists = (name: string) => T.Task /* * Public API @@ -36,18 +35,9 @@ export type CreateFactionError = FactionValidationError export type CreateFactionDependencies = { checkFactionNameExists: CheckFactionNameExists } -export type CreateFactionDependenciesT = { - checkFactionNameExists: CheckFactionNameExistsT -} export type CreateFaction = ( dependencies: CreateFactionDependencies ) => ( unvalidatedFaction: UnvalidatedFaction -) => E.Either - -export type CreateFactionT = ( - dependencies: CreateFactionDependenciesT -) => ( - unvalidatedFaction: UnvalidatedFaction ) => TE.TaskEither