Skip to content

Commit

Permalink
feat: add nullable patch fields
Browse files Browse the repository at this point in the history
  • Loading branch information
wmakeev committed Aug 4, 2021
1 parent ab609c6 commit 903853a
Show file tree
Hide file tree
Showing 20 changed files with 89 additions and 68 deletions.
15 changes: 7 additions & 8 deletions src/model/AbstractDemand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
EntityRef,
HasVat
} from '.'
import type { PartialNullable } from '../tools'

export type DemandMetaType = 'demand' | 'retaildemand'

Expand All @@ -17,12 +18,10 @@ export type AbstractDemandFields = {
// TODO shipmentOut: ShipmentOut
}

export type AbstractDemand<
T extends DemandMetaType
> = DocumentWithPositions<T> & HasVat
export type AbstractDemand<T extends DemandMetaType> =
DocumentWithPositions<T> & HasVat

export type AbstractDemandPatch<
T extends DemandMetaType
> = DocumentWithPositionsPatch<T> &
Partial<Pick<AbstractDemandFields, 'customerOrder'>> &
Partial<HasVat>
export type AbstractDemandPatch<T extends DemandMetaType> =
DocumentWithPositionsPatch<T> &
PartialNullable<Pick<AbstractDemandFields, 'customerOrder'>> &
Partial<HasVat>
3 changes: 2 additions & 1 deletion src/model/AbstractGood.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
ProductFolderFields,
TaxSystem
} from '.'
import type { PartialNullable } from '../tools'
import type { Attribute } from './Attribute'
import type { Barcode } from './Barcode'
import type { CollectionRef } from './CollectionRef'
Expand Down Expand Up @@ -176,7 +177,7 @@ export type AbstractGoodFields = ProductFolderFields & {
export type AbstractGood<T extends GoodMetaType> = Entity<T> &
AbstractGoodFields

export type AbstractGoodPatch = Partial<
export type AbstractGoodPatch = PartialNullable<
Pick<
AbstractGoodFields,
| 'uom'
Expand Down
3 changes: 2 additions & 1 deletion src/model/Attribute.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { PartialNullable } from '../tools'
import type { Entity } from './Entity'
import type { EntityPatchRef, EntityRef } from './EntityRef'
import type { MediaType } from './MediaType'
Expand Down Expand Up @@ -102,4 +103,4 @@ export type AttributePatch<T extends AttributeType = AttributeType> =
value: EntityRef<'customentity'>
}

: EntityPatchRef<'attributemetadata'> & Partial<Pick<Attribute<T>, 'value'>>
: EntityPatchRef<'attributemetadata'> & PartialNullable<Pick<Attribute<T>, 'value'>>
17 changes: 11 additions & 6 deletions src/model/Contract.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import type { Entity } from '.'
import type { Attribute, AttributePatch } from './Attribute'
import type { EntityRef } from './EntityRef'
import type { Owned, OwnedPatch } from './Owned'
import type { Rate } from './Rate'
import type {
Attribute,
AttributePatch,
Entity,
EntityRef,
Owned,
OwnedPatch,
Rate
} from '.'
import type { PartialNullable } from '../tools'

type ContractTypeFileds =
| {
Expand Down Expand Up @@ -82,7 +87,7 @@ type ContractFields = ContractTypeFileds & {

export type Contract = Entity<'contract'> & Owned & ContractFields

export type ContractPatch = Partial<
export type ContractPatch = PartialNullable<
Pick<
ContractFields,
| 'name'
Expand Down
3 changes: 2 additions & 1 deletion src/model/CustomerOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
OrderPatch,
TaxSystem
} from '.'
import type { PartialNullable } from '../tools'

export type CustomerOrderFields = HasVat & {
taxSystem?: TaxSystem
Expand Down Expand Up @@ -35,5 +36,5 @@ export type CustomerOrderExpand = Pick<
OrderExpand<'customerorder'>

export type CustomerOrderPatch = OrderPatch<'customerorder'> &
Partial<Pick<CustomerOrderFields, 'taxSystem'>> &
PartialNullable<Pick<CustomerOrderFields, 'taxSystem'>> &
Partial<HasVat>
8 changes: 6 additions & 2 deletions src/model/CustomerOrderPosition.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { PartialNullable } from '../tools'
import type { Position, PositionPatch } from './Position'

export type CustomerOrderPositionFields = {
Expand All @@ -17,5 +18,8 @@ export type CustomerOrderPositionFields = {
export type CustomerOrderPosition = Position<'customerorderposition'> &
CustomerOrderPositionFields

export type CustomerOrderPositionPatch = PositionPatch<'customerorderposition'> &
Partial<Pick<CustomerOrderPositionFields, 'vat' | 'reserve' | 'shipped'>>
export type CustomerOrderPositionPatch =
PositionPatch<'customerorderposition'> &
PartialNullable<
Pick<CustomerOrderPositionFields, 'vat' | 'reserve' | 'shipped'>
>
2 changes: 1 addition & 1 deletion src/model/Demand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export type DemandFields = {
export type Demand = AbstractDemand<'demand'>

export type DemandPatch = AbstractDemandPatch<'demand'>
// & Partial<Pick<DemandFields, ''>>
// & PartialNullable<Pick<DemandFields, ''>>
3 changes: 2 additions & 1 deletion src/model/DemandPosition.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { PartialNullable } from '../tools'
import type { Position, PositionPatch } from './Position'

export type DemandPositionFields = {
Expand All @@ -12,4 +13,4 @@ export type DemandPositionFields = {
export type DemandPosition = Position<'demandposition'> & DemandPositionFields

export type DemandPositionPatch = PositionPatch<'demandposition'> &
Partial<Pick<DemandPositionFields, 'vat' | 'overhead'>>
PartialNullable<Pick<DemandPositionFields, 'vat' | 'overhead'>>
12 changes: 6 additions & 6 deletions src/model/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
Rate,
Owned
} from '.'
import type { PartialNullable } from '../tools'
import type { OwnedPatch } from './Owned'

// TODO | 'processingplan'
Expand Down Expand Up @@ -128,10 +129,9 @@ export type DocumentExpand<T extends DocumentMetaType> = Pick<
| 'state'
>

export type DocumentPatch = Partial<
export type DocumentPatch = PartialNullable<
Pick<
DocumentFieds,
| 'agent'
| 'agentAccount'
| 'applicable'
| 'contract'
Expand All @@ -141,11 +141,11 @@ export type DocumentPatch = Partial<
| 'files'
| 'moment'
| 'name'
| 'organization'
| 'organizationAccount'
| 'project'
| 'state'
> & {
attributes: AttributePatch[]
>
> &
Partial<Pick<DocumentFieds, 'agent' | 'organization'>> & {
attributes?: AttributePatch[]
} & OwnedPatch
>
20 changes: 10 additions & 10 deletions src/model/DocumentWithPositions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { PartialNullable } from '../tools'
import type { CollectionRef } from './CollectionRef'
import type { Document, DocumentExpand, DocumentPatch } from './Document'
import type { EntityRef } from './EntityRef'
Expand All @@ -13,9 +14,8 @@ export type DocumentWithPositionsFields<
positions: CollectionRef<DocumentPositionType[T]>
}

export type DocumentWithPositions<
T extends DocumentWithPositionsMetaType
> = Document<T> & DocumentWithPositionsFields<T>
export type DocumentWithPositions<T extends DocumentWithPositionsMetaType> =
Document<T> & DocumentWithPositionsFields<T>

export type DocumentWithPositionsExpand<
T extends DocumentWithPositionsMetaType
Expand All @@ -25,10 +25,10 @@ export type DocumentWithPositionsExpand<
export type DocumentWithPositionsPatch<
T extends DocumentWithPositionsMetaType
> = DocumentPatch &
Partial<
{
// TODO Добавить вариант с коллекцией?
// TODO Для каждого типа документа будет свой PositionPatch (брать из type map)
positions: Patch<DocumentPositionType[T]>[]
} & Pick<DocumentWithPositionsFields<T>, 'store'>
>
// FIXME Где-то store не может быть null
PartialNullable<Pick<DocumentWithPositionsFields<T>, 'store'>> &
Partial<{
// TODO Добавить вариант с коллекцией?
// TODO Для каждого типа документа будет свой PositionPatch (брать из type map)
positions: Patch<DocumentPositionType[T]>[]
}>
9 changes: 5 additions & 4 deletions src/model/Finance.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Document, DocumentPatch } from './Document'
import type { EntityRef } from './EntityRef'
import type { MetaType } from './MetaType'
import type { Document, DocumentPatch, EntityRef, MetaType } from '.'
import type { PartialNullable } from '../tools'

// TODO FinaceOperation: наложить ограничения на возможные MetaType
export interface FinaceOperation extends EntityRef<MetaType> {
Expand All @@ -24,4 +23,6 @@ export type FinanceFields = {
export type Finance<T extends FinanceMetaType> = Document<T> & FinanceFields

export type FinancePatch = DocumentPatch &
Partial<Pick<FinanceFields, 'operations' | 'paymentPurpose' | 'vatSum'>>
PartialNullable<
Pick<FinanceFields, 'operations' | 'paymentPurpose' | 'vatSum'>
>
10 changes: 5 additions & 5 deletions src/model/Invoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
DocumentWithPositionsPatch,
HasVat
} from '.'
import type { PartialNullable } from '../tools'

export type InvoiceMetaType = 'invoicein' | 'invoiceout'

Expand All @@ -18,8 +19,7 @@ export type Invoice<T extends InvoiceMetaType> = DocumentWithPositions<T> &
InvoiceFields &
HasVat

export type InvoicePatch<
T extends InvoiceMetaType
> = DocumentWithPositionsPatch<T> &
Partial<Pick<InvoiceFields, 'paymentPlannedMoment'>> &
Partial<HasVat>
export type InvoicePatch<T extends InvoiceMetaType> =
DocumentWithPositionsPatch<T> &
PartialNullable<Pick<InvoiceFields, 'paymentPlannedMoment'>> &
Partial<HasVat>
3 changes: 2 additions & 1 deletion src/model/InvoiceIn.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { EntityRef, Invoice, InvoicePatch } from '.'
import type { PartialNullable } from '../tools'

export type InvoiceInFields = {
purchaseOrder?: EntityRef<'purchaseorder'>
Expand All @@ -12,4 +13,4 @@ export type InvoiceInFields = {
export type InvoiceIn = Invoice<'invoicein'>

export type InvoiceInPatch = InvoicePatch<'invoicein'> &
Partial<Pick<InvoiceInFields, 'purchaseOrder'>>
PartialNullable<Pick<InvoiceInFields, 'purchaseOrder'>>
3 changes: 2 additions & 1 deletion src/model/InvoiceOut.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { EntityRef, Invoice, InvoicePatch } from '.'
import type { PartialNullable } from '../tools'

export type InvoiceOutFields = {
customerOrder?: EntityRef<'customerorder'>
Expand All @@ -10,4 +11,4 @@ export type InvoiceOutFields = {
export type InvoiceOut = Invoice<'invoiceout'> & InvoiceOutFields

export type InvoiceOutPatch = InvoicePatch<'invoiceout'> &
Partial<Pick<InvoiceOutFields, 'customerOrder'>>
PartialNullable<Pick<InvoiceOutFields, 'customerOrder'>>
3 changes: 2 additions & 1 deletion src/model/InvoicePosition.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { PartialNullable } from '../tools'
import type { Position, PositionPatch } from './Position'

export type InvoicePositionFields = {
Expand All @@ -11,4 +12,4 @@ export type InvoicePosition = Position<'invoiceposition'> &
InvoicePositionFields

export type InvoicePositionPatch = PositionPatch<'invoiceposition'> &
Partial<Pick<InvoicePositionFields, 'vat'>>
PartialNullable<Pick<InvoicePositionFields, 'vat'>>
8 changes: 4 additions & 4 deletions src/model/Order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
DocumentWithPositionsPatch,
EntityRef
} from '.'
import type { PartialNullable } from '../tools'

export type OrderMetaType = 'customerorder' | 'purchaseorder'

Expand All @@ -21,7 +22,6 @@ export type Order<T extends OrderMetaType> = DocumentWithPositions<T> &
export type OrderExpand<T extends OrderMetaType> = Pick<Order<T>, 'store'> &
DocumentWithPositionsExpand<T>

export type OrderPatch<
T extends OrderMetaType
> = DocumentWithPositionsPatch<T> &
Partial<Pick<OrderFields, 'deliveryPlannedMoment' | 'store'>>
export type OrderPatch<T extends OrderMetaType> =
DocumentWithPositionsPatch<T> &
PartialNullable<Pick<OrderFields, 'deliveryPlannedMoment' | 'store'>>
7 changes: 4 additions & 3 deletions src/model/Position.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { PartialNullable } from '../tools'
import type { Entity } from './Entity'
import type { EntityPatchRef, EntityRef } from './EntityRef'

Expand Down Expand Up @@ -57,6 +58,6 @@ export interface Position<T extends PositionMetaType> extends Entity<T> {
}

export type PositionPatch<T extends PositionMetaType> = Partial<
EntityPatchRef<T> &
Pick<Position<T>, 'quantity' | 'price' | 'discount' | 'assortment'>
>
EntityPatchRef<T> & Pick<Position<T>, 'quantity' | 'price'>
> &
PartialNullable<Pick<Position<T>, 'assortment' | 'discount'>>
24 changes: 13 additions & 11 deletions src/model/ProductFolder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Entity, EntityRef, Owned, OwnedPatch, TaxSystem } from '.'
import type { PartialNullable } from '../tools'

export type ProductFolderFields = Owned & {
name: string
Expand Down Expand Up @@ -30,16 +31,17 @@ export type ProductFolderFields = Owned & {
export type ProductFolder = Entity<'productfolder'> & ProductFolderFields

export type ProductFolderPatch = Partial<
Pick<
ProductFolderFields,
| 'name'
| 'description'
| 'code'
| 'externalCode'
| 'archived'
| 'vat'
| 'productFolder'
| 'taxSystem'
>
Pick<ProductFolderFields, 'name' | 'archived'>
> &
PartialNullable<
Pick<
ProductFolderFields,
| 'description'
| 'code'
| 'externalCode'
| 'vat'
| 'productFolder'
| 'taxSystem'
>
> &
OwnedPatch
2 changes: 2 additions & 0 deletions src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ export function isType<T extends MetaType>(
): entity is EntityByMetaType[T] {
return (entity as any)?.meta?.type === metaType
}

export type PartialNullable<T> = { [P in keyof T]?: T[P] | undefined | null }
2 changes: 1 addition & 1 deletion test/model/common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { testTypeEqual } from '../tools'

const customerorder: Patch<'customerorder'> = {}

testTypeEqual<number | undefined>(customerorder.positions?.[0].reserve)
testTypeEqual<number | undefined | null>(customerorder.positions?.[0].reserve)

const invoiceOut: Patch<'invoiceout'> = {}

Expand Down

0 comments on commit 903853a

Please sign in to comment.