Skip to content

Commit

Permalink
feat(model): add Contract
Browse files Browse the repository at this point in the history
  • Loading branch information
wmakeev committed Jul 26, 2021
1 parent 68ffe96 commit d022e36
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "moysklad-api-model",
"version": "0.3.7",
"version": "0.3.8",
"description": "Объектная модель API МойСклад для TypeScript проектов",
"main": "build/src/index.js",
"types": "build/src/index.d.ts",
Expand Down
103 changes: 103 additions & 0 deletions src/model/Contract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
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'

type ContractTypeFileds =
| {
/**
* Тип Договора.
*
* Возможные значения:
* - Договор комиссии
* - Договор купли-продажи
*/
contractType: 'Sales'
}
| {
contractType: 'Commission'

/**
* Тип Вознаграждения.
*
* Указывается когда `contractType` = `Commission`
*
* Возможные значения:
* - Процент от суммы продажи
* - Не рассчитывать
* */
rewardType: 'None'
}
| {
contractType: 'Commission'

/**
* Тип Вознаграждения.
*
* Указывается когда `contractType` = `Commission`
*
* Возможные значения:
* - Процент от суммы продажи
* - Не рассчитывать
* */
rewardType: 'PercentOfSales'

/** Вознаграждение в процентах (от 0 до 100) */
rewardPercent: number
}

type ContractFields = ContractTypeFileds & {
readonly updated: string

name: string

description?: string

code?: string

externalCode?: string

archived: boolean

moment: string

/** Сумма Договора */
sum: number

ownAgent: EntityRef<'organization'>

agent: EntityRef<'counterparty' | 'organization'>

state?: EntityRef<'state'>

organizationAccount: EntityRef<'account'>

agentAccount?: EntityRef<'account'>

rate: Rate

attributes: Attribute[]
}

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

export type ContractPatch = Partial<
Pick<
ContractFields,
| 'name'
| 'description'
| 'code'
| 'externalCode'
| 'archived'
| 'ownAgent'
| 'agent'
| 'state'
| 'organizationAccount'
| 'agentAccount'
| 'rate'
> &
ContractTypeFileds
> & {
attributes?: AttributePatch[]
} & OwnedPatch
3 changes: 2 additions & 1 deletion src/model/MetaType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
CashIn,
CashOut,
ContactPerson,
Contract,
Counterparty,
CustomerOrder,
CustomerOrderPosition,
Expand Down Expand Up @@ -140,7 +141,7 @@ export type EntityByMetaType = {
companysettings: Entity<'companysettings'>
consignment: Entity<'consignment'>
contactperson: ContactPerson
contract: Entity<'contract'>
contract: Contract
counterparty: Counterparty
country: Entity<'country'>
currency: Entity<'currency'>
Expand Down
25 changes: 13 additions & 12 deletions src/model/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
// Добавить новые сущности аналогично в MetaType.ts

// Model
export * from './AbstractDemand'
export * from './AbstractFile'
export * from './AbstractGood'
export * from './Account'
export * from './Address'
export * from './Agent'
export * from './AgentNote'
export * from './Attribute'
export * from './Barcode'
export * from './CashIn'
export * from './CashOut'
export * from './Collection'
export * from './CollectionRef'
export * from './Company'
export * from './ContactPerson'
export * from './Contract'
export * from './Counterparty'
export * from './CustomerOrder'
export * from './CustomerOrderPosition'
Expand All @@ -23,17 +28,19 @@ export * from './DocumentWithPositions'
export * from './Employee'
export * from './Entity'
export * from './EntityRef'
export * from './File'
export * from './Group'
export * from './HasAttributes'
export * from './HasCreated'
export * from './HasDeleted'
export * from './HasFiles'
export * from './HasProject'
export * from './Rate'
export * from './HasState'
export * from './HasStore'
export * from './HasUpdated'
export * from './HasVat'
export * from './Id'
export * from './Image'
export * from './Invoice'
export * from './InvoiceIn'
export * from './InvoiceOut'
Expand All @@ -46,25 +53,19 @@ export * from './MoyskladObject'
export * from './Order'
export * from './Organization'
export * from './Owned'
export * from './Pack'
export * from './PaymentIn'
export * from './PaymentOut'
export * from './Person'
export * from './Position'
export * from './Price'
export * from './PriceType'
export * from './Product'
export * from './ProductFolder'
export * from './Rate'
export * from './RetailDemand'
export * from './State'
export * from './TaxSystem'
export * from './Id'
export * from './AbstractDemand'
export * from './AbstractGood'
export * from './Image'
export * from './Pack'
export * from './Barcode'
export * from './AbstractFile'
export * from './File'
export * from './Price'
export * from './Product'
export * from './ProductFolder'

// Utility types
export * from './utils'
Expand Down
2 changes: 2 additions & 0 deletions src/model/utils/Patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
AttributePatch,
CashInPatch,
CashOutPatch,
ContractPatch,
CustomerOrderPatch,
CustomerOrderPositionPatch,
DemandPatch,
Expand All @@ -20,6 +21,7 @@ export type PatchByMetaType = {
attributemetadata: AttributePatch
cashin: CashInPatch
cashout: CashOutPatch
contract: ContractPatch
customerorder: CustomerOrderPatch
customerorderposition: CustomerOrderPositionPatch
demand: DemandPatch
Expand Down
33 changes: 33 additions & 0 deletions test/model/Contract.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { Contract, Patch } from '../../src'

const t10 = {} as Contract

// @ts-expect-error
t10.contractType.rewardType

if (t10.contractType === 'Commission') {
t10.rewardType = 'PercentOfSales'

// @ts-expect-error
t10.rewardPercent

if (t10.rewardType === 'PercentOfSales') {
t10.rewardPercent = 50
}
}

const t20 = {} as Patch<'contract'>

// @ts-expect-error
t20.contractType.rewardType

if (t20.contractType === 'Commission') {
t20.rewardType = 'PercentOfSales'

// @ts-expect-error
t20.rewardPercent

if (t20.rewardType === 'PercentOfSales') {
t20.rewardPercent = 50
}
}

0 comments on commit d022e36

Please sign in to comment.