@things-factory/sales 10.1.232 → 10.1.234
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist-client/tsconfig.tsbuildinfo +1 -1
- package/dist-server/service/opportunity/opportunity-service.js +1 -1
- package/dist-server/service/opportunity/opportunity-service.js.map +1 -1
- package/dist-server/service/opportunity/opportunity-type.js +1 -1
- package/dist-server/service/opportunity/opportunity-type.js.map +1 -1
- package/dist-server/service/sales-product/sales-product-service.js +3 -0
- package/dist-server/service/sales-product/sales-product-service.js.map +1 -1
- package/dist-server/tsconfig.tsbuildinfo +1 -1
- package/package.json +7 -7
- package/translations/en.json +2 -1
- package/translations/es.json +2 -1
- package/translations/ja.json +2 -1
- package/translations/ko.json +2 -1
- package/translations/ms.json +2 -1
- package/translations/zh.json +2 -1
|
@@ -39,7 +39,7 @@ async function saveOpportunity(tx, domain, user, input, id) {
|
|
|
39
39
|
if (!(0, opportunity_rules_1.validMonth)(input.expectedCloseMonth))
|
|
40
40
|
throw new shell_1.Refusal('opportunity-close-month-invalid', {}, 'Give the expected month as YYYY-MM');
|
|
41
41
|
if (input.probability !== undefined && input.probability !== null && !(0, opportunity_rules_1.validProbability)(input.probability))
|
|
42
|
-
throw new shell_1.Refusal('opportunity-probability-invalid', {}, 'The probability is a whole number from 0 to 100');
|
|
42
|
+
throw new shell_1.Refusal('opportunity-probability-invalid', { value: String(input.probability), min: 0, max: 100 }, 'The probability is a whole number from 0 to 100');
|
|
43
43
|
const counterparties = tx.getRepository(biz_base_1.Counterparty);
|
|
44
44
|
const counterparty = input.counterpartyId ? await counterparties.findOne({ where: { id: input.counterpartyId, domain: { id: domain.id } } }) : null;
|
|
45
45
|
if (!counterparty)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"opportunity-service.js","sourceRoot":"","sources":["../../../server/service/opportunity/opportunity-service.ts"],"names":[],"mappings":";;AAYA,kDAIC;AAMD,0CAuDC;AAMD,4CAsBC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AM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,KAAK,oCAAgB,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,SAAS,CAAC,CAAA;IACxI,OAAO,MAAM,CAAA;AACf,CAAC;AAED,SAAS,sBAAsB,CAAC,EAAiB,EAAE,MAAc,EAAE,IAAS,EAAE,WAAwB,EAAE,MAAc,EAAE,SAA4B,EAAE,MAAe;IACnK,OAAO,IAAA,8BAAgB,EACrB,EAAE,EACF,oCAAgB,EAChB,EAAE,WAAW,EAAE,SAAS,EAAE,SAAS,IAAI,IAAI,EAAE,KAAK,EAAE,WAAW,CAAC,KAAK,EAAE,WAAW,EAAE,WAAW,CAAC,WAAW,EAAE,EAC7G,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,UAAU,CAAC,WAAW,CAAC,EAAE,CACpE,CAAA;AACH,CAAC;AAED,mCAAmC;AACnC,SAAS,UAAU,CAAC,WAAwB;IAC1C,MAAM,IAAI,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,gBAAgB,EAAE,eAAe,EAAE,QAAQ,EAAE,UAAU,EAAE,oBAAoB,EAAE,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,UAAU,EAAE,YAAY,EAAE,aAAa,CAAC,CAAA;IACtL,OAAO,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,GAAG,EAAE,WAAW,CAAC,GAAG,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC,CAAA;AAC7E,CAAC","sourcesContent":["import { getDomainAssignees } from '@things-factory/auth-base'\nimport { Counterparty } from '@things-factory/biz-base'\nimport { currencyDigits, validDay } from '@things-factory/invoice'\nimport { Domain, Refusal, calendarDay } from '@things-factory/shell'\nimport { EntityManager } from 'typeorm'\n\nimport { recordSalesEvent } from '../sales-event'\nimport { Opportunity } from './opportunity'\nimport { OpportunityEvent } from './opportunity-event'\nimport { isOpen, OpportunityStage, probabilityAfter, stageMove, validMonth, validProbability } from './opportunity-rules'\nimport { NewOpportunity } from './opportunity-type'\n\nexport async function opportunityInDomain(tx: EntityManager, domain: Domain, id: string) {\n const opportunity = await tx.getRepository(Opportunity).findOne({ where: { id, domain: { id: domain.id } } })\n if (!opportunity) throw new Refusal('opportunity-not-found', {}, 'Opportunity not found')\n return opportunity\n}\n\n/**\n * Create an opportunity, or change one a person may still change. An open one takes any open stage; a lost one is\n * opened again by giving it an open stage. Closing is `closeOpportunity`'s.\n */\nexport async function saveOpportunity(tx: EntityManager, domain: Domain, user: any, input: NewOpportunity, id?: string) {\n const old = id ? await opportunityInDomain(tx, domain, id) : undefined\n const stage = input.stage ?? old?.stage ?? OpportunityStage.LEAD\n /* A closed one changes only by being opened again, and only a lost one may be. */\n if (old && !isOpen(old.stage) && !(isOpen(stage) && stageMove(old.stage, stage))) throw new Refusal('opportunity-closed', {}, 'A closed opportunity is not changed')\n if (!isOpen(stage)) throw new Refusal('opportunity-stage-open-required', {}, 'Give an open stage; won and lost are closings')\n\n const name = input.name?.trim()\n if (!name) throw new Refusal('opportunity-name-required', {}, 'Name the opportunity')\n if (!Number.isFinite(input.amount) || input.amount < 0) throw new Refusal('opportunity-amount-invalid', {}, 'The expected amount is zero or more')\n const currency = (input.currency?.trim() || 'KRW').toUpperCase()\n currencyDigits(currency)\n if (!validMonth(input.expectedCloseMonth)) throw new Refusal('opportunity-close-month-invalid', {}, 'Give the expected month as YYYY-MM')\n if (input.probability !== undefined && input.probability !== null && !validProbability(input.probability))\n throw new Refusal('opportunity-probability-invalid', {}, 'The probability is a whole number from 0 to 100')\n\n const counterparties = tx.getRepository(Counterparty)\n const counterparty = input.counterpartyId ? await counterparties.findOne({ where: { id: input.counterpartyId, domain: { id: domain.id } } }) : null\n if (!counterparty) throw new Refusal('opportunity-counterparty-required', {}, 'Choose the counterparty of this domain')\n const endCustomer = input.endCustomerId ? await counterparties.findOne({ where: { id: input.endCustomerId, domain: { id: domain.id } } }) : null\n if (input.endCustomerId && !endCustomer) throw new Refusal('opportunity-end-customer-invalid', {}, 'Choose the end customer among the counterparties of this domain')\n\n const ownerId = input.ownerId || old?.ownerId || user.id\n /* The people work here may be given to — auth-base's one answer, the same a license's recipient gets. */\n if (ownerId !== user.id && ownerId !== old?.ownerId && !(await getDomainAssignees(domain, tx)).some(one => one.id === ownerId))\n throw new Refusal('opportunity-owner-invalid', {}, 'Put it in the charge of a member of this domain')\n\n const staged = !!old && old.stage !== stage\n /* The probability follows the stage unless the person gave one; with the stage unchanged it stays as it was. */\n const probability = !old || staged ? probabilityAfter(stage, input.probability) : input.probability ?? old.probability\n\n const repository = tx.getRepository(Opportunity)\n const row = await repository.save(\n repository.create({\n ...(old ? { id: old.id, creator: old.creatorId ? ({ id: old.creatorId } as any) : undefined } : { creator: user }),\n domain,\n name,\n counterparty,\n endCustomer: endCustomer ?? null,\n amount: input.amount,\n currency,\n expectedCloseMonth: input.expectedCloseMonth,\n stage,\n probability,\n owner: { id: ownerId } as any,\n /* A lost opportunity opened again is no longer closed. */\n closedOn: null,\n lostReason: null,\n description: input.description?.trim() || null,\n updater: user\n })\n )\n const saved = await opportunityInDomain(tx, domain, row.id)\n await recordOpportunityEvent(tx, domain, user, saved, !old ? 'CREATED' : staged ? 'STAGED' : 'UPDATED', staged ? old.stage : undefined)\n return saved\n}\n\n/**\n * Close an open opportunity — lost, with the reason, by a person; won by the application that turned it into its own\n * record (Office's contract, ADR-0138 decision 8). The day is the domain's today unless one is given.\n */\nexport async function closeOpportunity(\n tx: EntityManager,\n domain: Domain,\n user: any,\n id: string,\n outcome: OpportunityStage.WON | OpportunityStage.LOST,\n { closedOn, reason }: { closedOn?: string; reason?: string } = {}\n) {\n const opportunity = await opportunityInDomain(tx, domain, id)\n if (!stageMove(opportunity.stage, outcome)) throw new Refusal('opportunity-stage-move-invalid', {}, 'The opportunity cannot move to that stage')\n if (outcome === OpportunityStage.LOST && !reason?.trim()) throw new Refusal('opportunity-reason-required', {}, 'Say why it was lost')\n const day = closedOn || calendarDay(domain.timezone)\n if (!validDay(day)) throw new Refusal('opportunity-closed-on-invalid', {}, 'Give the closing day as YYYY-MM-DD')\n\n const from = opportunity.stage\n await tx.getRepository(Opportunity).update(\n { id: opportunity.id },\n { stage: outcome, probability: probabilityAfter(outcome), closedOn: day, lostReason: outcome === OpportunityStage.LOST ? reason!.trim() : null, updater: user }\n )\n const closed = await opportunityInDomain(tx, domain, id)\n await recordOpportunityEvent(tx, domain, user, closed, outcome, from, outcome === OpportunityStage.LOST ? closed.lostReason : undefined)\n return closed\n}\n\nfunction recordOpportunityEvent(tx: EntityManager, domain: Domain, user: any, opportunity: Opportunity, action: string, fromStage?: OpportunityStage, reason?: string) {\n return recordSalesEvent(\n tx,\n OpportunityEvent,\n { opportunity, fromStage: fromStage ?? null, stage: opportunity.stage, probability: opportunity.probability },\n { domain, user, action, reason, snapshot: snapshotOf(opportunity) }\n )\n}\n\n/** The opportunity as it stood. */\nfunction snapshotOf(opportunity: Opportunity) {\n const keys = ['id', 'name', 'counterpartyId', 'endCustomerId', 'amount', 'currency', 'expectedCloseMonth', 'stage', 'probability', 'ownerId', 'closedOn', 'lostReason', 'description']\n return Object.fromEntries(keys.map(key => [key, opportunity[key] ?? null]))\n}\n"]}
|
|
1
|
+
{"version":3,"file":"opportunity-service.js","sourceRoot":"","sources":["../../../server/service/opportunity/opportunity-service.ts"],"names":[],"mappings":";;AAYA,kDAIC;AAMD,0CAuDC;AAMD,4CAsBC;AAzGD,yDAA8D;AAC9D,uDAAuD;AACvD,qDAAkE;AAClE,iDAAoE;AAGpE,gDAAiD;AACjD,+CAA2C;AAC3C,2DAAsD;AACtD,2DAAyH;AAGlH,KAAK,UAAU,mBAAmB,CAAC,EAAiB,EAAE,MAAc,EAAE,EAAU;IACrF,MAAM,WAAW,GAAG,MAAM,EAAE,CAAC,aAAa,CAAC,yBAAW,CAA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,CAAC,CAAA;IACxI,OAAO,MAAM,CAAA;AACf,CAAC;AAED,SAAS,sBAAsB,CAAC,EAAiB,EAAE,MAAc,EAAE,IAAS,EAAE,WAAwB,EAAE,MAAc,EAAE,SAA4B,EAAE,MAAe;IACnK,OAAO,IAAA,8BAAgB,EACrB,EAAE,EACF,oCAAgB,EAChB,EAAE,WAAW,EAAE,SAAS,EAAE,SAAS,IAAI,IAAI,EAAE,KAAK,EAAE,WAAW,CAAC,KAAK,EAAE,WAAW,EAAE,WAAW,CAAC,WAAW,EAAE,EAC7G,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,UAAU,CAAC,WAAW,CAAC,EAAE,CACpE,CAAA;AACH,CAAC;AAED,mCAAmC;AACnC,SAAS,UAAU,CAAC,WAAwB;IAC1C,MAAM,IAAI,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,gBAAgB,EAAE,eAAe,EAAE,QAAQ,EAAE,UAAU,EAAE,oBAAoB,EAAE,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,UAAU,EAAE,YAAY,EAAE,aAAa,CAAC,CAAA;IACtL,OAAO,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,GAAG,EAAE,WAAW,CAAC,GAAG,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC,CAAA;AAC7E,CAAC","sourcesContent":["import { getDomainAssignees } from '@things-factory/auth-base'\nimport { Counterparty } from '@things-factory/biz-base'\nimport { currencyDigits, validDay } from '@things-factory/invoice'\nimport { Domain, Refusal, calendarDay } from '@things-factory/shell'\nimport { EntityManager } from 'typeorm'\n\nimport { recordSalesEvent } from '../sales-event'\nimport { Opportunity } from './opportunity'\nimport { OpportunityEvent } from './opportunity-event'\nimport { isOpen, OpportunityStage, probabilityAfter, stageMove, validMonth, validProbability } from './opportunity-rules'\nimport { NewOpportunity } from './opportunity-type'\n\nexport async function opportunityInDomain(tx: EntityManager, domain: Domain, id: string) {\n const opportunity = await tx.getRepository(Opportunity).findOne({ where: { id, domain: { id: domain.id } } })\n if (!opportunity) throw new Refusal('opportunity-not-found', {}, 'Opportunity not found')\n return opportunity\n}\n\n/**\n * Create an opportunity, or change one a person may still change. An open one takes any open stage; a lost one is\n * opened again by giving it an open stage. Closing is `closeOpportunity`'s.\n */\nexport async function saveOpportunity(tx: EntityManager, domain: Domain, user: any, input: NewOpportunity, id?: string) {\n const old = id ? await opportunityInDomain(tx, domain, id) : undefined\n const stage = input.stage ?? old?.stage ?? OpportunityStage.LEAD\n /* A closed one changes only by being opened again, and only a lost one may be. */\n if (old && !isOpen(old.stage) && !(isOpen(stage) && stageMove(old.stage, stage))) throw new Refusal('opportunity-closed', {}, 'A closed opportunity is not changed')\n if (!isOpen(stage)) throw new Refusal('opportunity-stage-open-required', {}, 'Give an open stage; won and lost are closings')\n\n const name = input.name?.trim()\n if (!name) throw new Refusal('opportunity-name-required', {}, 'Name the opportunity')\n if (!Number.isFinite(input.amount) || input.amount < 0) throw new Refusal('opportunity-amount-invalid', {}, 'The expected amount is zero or more')\n const currency = (input.currency?.trim() || 'KRW').toUpperCase()\n currencyDigits(currency)\n if (!validMonth(input.expectedCloseMonth)) throw new Refusal('opportunity-close-month-invalid', {}, 'Give the expected month as YYYY-MM')\n if (input.probability !== undefined && input.probability !== null && !validProbability(input.probability))\n throw new Refusal('opportunity-probability-invalid', { value: String(input.probability), min: 0, max: 100 }, 'The probability is a whole number from 0 to 100')\n\n const counterparties = tx.getRepository(Counterparty)\n const counterparty = input.counterpartyId ? await counterparties.findOne({ where: { id: input.counterpartyId, domain: { id: domain.id } } }) : null\n if (!counterparty) throw new Refusal('opportunity-counterparty-required', {}, 'Choose the counterparty of this domain')\n const endCustomer = input.endCustomerId ? await counterparties.findOne({ where: { id: input.endCustomerId, domain: { id: domain.id } } }) : null\n if (input.endCustomerId && !endCustomer) throw new Refusal('opportunity-end-customer-invalid', {}, 'Choose the end customer among the counterparties of this domain')\n\n const ownerId = input.ownerId || old?.ownerId || user.id\n /* The people work here may be given to — auth-base's one answer, the same a license's recipient gets. */\n if (ownerId !== user.id && ownerId !== old?.ownerId && !(await getDomainAssignees(domain, tx)).some(one => one.id === ownerId))\n throw new Refusal('opportunity-owner-invalid', {}, 'Put it in the charge of a member of this domain')\n\n const staged = !!old && old.stage !== stage\n /* The probability follows the stage unless the person gave one; with the stage unchanged it stays as it was. */\n const probability = !old || staged ? probabilityAfter(stage, input.probability) : input.probability ?? old.probability\n\n const repository = tx.getRepository(Opportunity)\n const row = await repository.save(\n repository.create({\n ...(old ? { id: old.id, creator: old.creatorId ? ({ id: old.creatorId } as any) : undefined } : { creator: user }),\n domain,\n name,\n counterparty,\n endCustomer: endCustomer ?? null,\n amount: input.amount,\n currency,\n expectedCloseMonth: input.expectedCloseMonth,\n stage,\n probability,\n owner: { id: ownerId } as any,\n /* A lost opportunity opened again is no longer closed. */\n closedOn: null,\n lostReason: null,\n description: input.description?.trim() || null,\n updater: user\n })\n )\n const saved = await opportunityInDomain(tx, domain, row.id)\n await recordOpportunityEvent(tx, domain, user, saved, !old ? 'CREATED' : staged ? 'STAGED' : 'UPDATED', staged ? old.stage : undefined)\n return saved\n}\n\n/**\n * Close an open opportunity — lost, with the reason, by a person; won by the application that turned it into its own\n * record (Office's contract, ADR-0138 decision 8). The day is the domain's today unless one is given.\n */\nexport async function closeOpportunity(\n tx: EntityManager,\n domain: Domain,\n user: any,\n id: string,\n outcome: OpportunityStage.WON | OpportunityStage.LOST,\n { closedOn, reason }: { closedOn?: string; reason?: string } = {}\n) {\n const opportunity = await opportunityInDomain(tx, domain, id)\n if (!stageMove(opportunity.stage, outcome)) throw new Refusal('opportunity-stage-move-invalid', {}, 'The opportunity cannot move to that stage')\n if (outcome === OpportunityStage.LOST && !reason?.trim()) throw new Refusal('opportunity-reason-required', {}, 'Say why it was lost')\n const day = closedOn || calendarDay(domain.timezone)\n if (!validDay(day)) throw new Refusal('opportunity-closed-on-invalid', {}, 'Give the closing day as YYYY-MM-DD')\n\n const from = opportunity.stage\n await tx.getRepository(Opportunity).update(\n { id: opportunity.id },\n { stage: outcome, probability: probabilityAfter(outcome), closedOn: day, lostReason: outcome === OpportunityStage.LOST ? reason!.trim() : null, updater: user }\n )\n const closed = await opportunityInDomain(tx, domain, id)\n await recordOpportunityEvent(tx, domain, user, closed, outcome, from, outcome === OpportunityStage.LOST ? closed.lostReason : undefined)\n return closed\n}\n\nfunction recordOpportunityEvent(tx: EntityManager, domain: Domain, user: any, opportunity: Opportunity, action: string, fromStage?: OpportunityStage, reason?: string) {\n return recordSalesEvent(\n tx,\n OpportunityEvent,\n { opportunity, fromStage: fromStage ?? null, stage: opportunity.stage, probability: opportunity.probability },\n { domain, user, action, reason, snapshot: snapshotOf(opportunity) }\n )\n}\n\n/** The opportunity as it stood. */\nfunction snapshotOf(opportunity: Opportunity) {\n const keys = ['id', 'name', 'counterpartyId', 'endCustomerId', 'amount', 'currency', 'expectedCloseMonth', 'stage', 'probability', 'ownerId', 'closedOn', 'lostReason', 'description']\n return Object.fromEntries(keys.map(key => [key, opportunity[key] ?? null]))\n}\n"]}
|
|
@@ -37,7 +37,7 @@ tslib_1.__decorate([
|
|
|
37
37
|
tslib_1.__metadata("design:type", String)
|
|
38
38
|
], NewOpportunity.prototype, "stage", void 0);
|
|
39
39
|
tslib_1.__decorate([
|
|
40
|
-
(0, type_graphql_1.Field)(type => type_graphql_1.
|
|
40
|
+
(0, type_graphql_1.Field)(type => type_graphql_1.Float, { nullable: true, description: 'Chance of winning in percent, a whole number from 0 to 100 - another is refused naming it; the stage gives it when empty' }),
|
|
41
41
|
tslib_1.__metadata("design:type", Number)
|
|
42
42
|
], NewOpportunity.prototype, "probability", void 0);
|
|
43
43
|
tslib_1.__decorate([
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"opportunity-type.js","sourceRoot":"","sources":["../../../server/service/opportunity/opportunity-type.ts"],"names":[],"mappings":";;;;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,EAAE,sDAAsD,EAAE,CAAC;;8CAAc;0BAF/F,eAAe;IAD3B,IAAA,yBAAU,EAAC,EAAE,WAAW,EAAE,yBAAyB,EAAE,CAAC;GAC1C,eAAe,CAG3B;AAGM,IAAM,uBAAuB,GAA7B,MAAM,uBAAuB;CAInC,CAAA;AAJY,0DAAuB;AAC6B;IAA9D,IAAA,oBAAK,EAAC,IAAI,CAAC,EAAE,CAAC,oCAAgB,EAAE,EAAE,WAAW,EAAE,WAAW,EAAE,CAAC;;sDAAwB;AAC+B;IAApH,IAAA,oBAAK,EAAC,IAAI,CAAC,EAAE,CAAC,kBAAG,EAAE,EAAE,WAAW,EAAE,8EAA8E,EAAE,CAAC;;4DAAoB;AAC7B;IAA1G,IAAA,oBAAK,EAAC,EAAE,WAAW,EAAE,iFAAiF,EAAE,CAAC;;qDAAc;kCAH7G,uBAAuB;IADnC,IAAA,yBAAU,EAAC,EAAE,WAAW,EAAE,yDAAyD,EAAE,CAAC;GAC1E,uBAAuB,CAInC","sourcesContent":["import { Field, Float, InputType, Int, ObjectType } from 'type-graphql'\n\nimport { Opportunity } from './opportunity'\nimport { OpportunityStage } from './opportunity-rules'\n\n@InputType({ description: 'An opportunity to create, or what a person changes on one' })\nexport class NewOpportunity {\n @Field({ description: 'What the opportunity is called' }) name: string\n @Field({ description: 'Id of the counterparty the domain would contract with' }) counterpartyId: string\n @Field({ nullable: true, description: 'Id of the end customer, when the work comes through another party' }) endCustomerId?: string\n @Field(type => Float, { description: 'Expected amount before tax' }) amount: number\n @Field({ nullable: true, description: 'ISO 4217 currency code, KRW when empty' }) currency?: string\n @Field({ description: 'Month the domain expects to win it, YYYY-MM' }) expectedCloseMonth: string\n @Field(type => OpportunityStage, { nullable: true, description: 'An open stage; a new opportunity starts as a lead when empty' }) stage?: OpportunityStage\n @Field(type => Float, { nullable: true, description: 'Chance of winning in percent, a whole number from 0 to 100 - another is refused naming it; the stage gives it when empty' }) probability?: number\n @Field({ nullable: true, description: 'Id of the person in charge; the caller when empty' }) ownerId?: string\n @Field({ nullable: true, description: 'Notes' }) description?: string\n}\n\n@ObjectType({ description: 'A page of opportunities' })\nexport class OpportunityList {\n @Field(type => [Opportunity], { description: 'Opportunities on the page' }) items: Opportunity[]\n @Field(type => Int, { description: 'Number of opportunities matching the list parameters' }) total: number\n}\n\n@ObjectType({ description: 'A stage and the probability it starts an opportunity at' })\nexport class OpportunityStageDefault {\n @Field(type => OpportunityStage, { description: 'The stage' }) stage: OpportunityStage\n @Field(type => Int, { description: 'The probability the stage gives, in percent — a judgement, not a measurement' }) probability: number\n @Field({ description: 'Whether a person moves an opportunity through this stage; won and lost close it' }) open: boolean\n}\n"]}
|
|
@@ -40,6 +40,9 @@ function rowOf(patch, old) {
|
|
|
40
40
|
const price = patch.unitPrice ?? 0;
|
|
41
41
|
if (!Number.isFinite(price) || price < 0)
|
|
42
42
|
throw new shell_1.Refusal('sales-product-price-invalid', {}, 'The price is zero or more');
|
|
43
|
+
/* A quotation line takes a price to four decimals (invoice's document-amounts) - a finer one would be refused there. */
|
|
44
|
+
if (Math.abs(price * 10_000 - Math.round(price * 10_000)) > 1e-6)
|
|
45
|
+
throw new shell_1.Refusal('sales-product-price-decimals', { value: String(price), max: 4 }, 'unitPrice: at most four decimals');
|
|
43
46
|
row.unitPrice = price;
|
|
44
47
|
}
|
|
45
48
|
if (patch.taxKind !== undefined || !old)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sales-product-service.js","sourceRoot":"","sources":["../../../server/service/sales-product/sales-product-service.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"sales-product-service.js","sourceRoot":"","sources":["../../../server/service/sales-product/sales-product-service.ts"],"names":[],"mappings":";;AAmDA,8CA6BC;AAhFD,qDAAwD;AACxD,iDAAuD;AACvD,qCAA2C;AAE3C,mDAA8C;AAC9C,+DAAgE;AAGhE,MAAM,IAAI,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,aAAa,CAAU,CAAA;AAEt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C1C,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,IAAI,KAAK,GAAG,CAAC,IAAI,IAAI,GAAG,CAAC,EAAE,KAAK,GAAG,CAAC,EAAE,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,GAAG,KAAK,IAAI,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,IAAI,CAAC,CAAA;QAC9I,IAAI,KAAK;YAAE,MAAM,IAAI,eAAO,CAAC,8BAA8B,EAAE,EAAE,IAAI,EAAE,GAAG,CAAC,IAAI,EAAE,EAAE,gBAAgB,GAAG,CAAC,IAAI,oBAAoB,CAAC,CAAA;IAChI,CAAC;IAED,MAAM,KAAK,GAAmB,EAAE,CAAA;IAChC,KAAK,MAAM,GAAG,IAAI,IAAI;QAAE,KAAK,CAAC,IAAI,CAAC,MAAM,UAAU,CAAC,IAAI,CAAC,EAAE,GAAG,GAAG,EAAE,MAAM,EAAE,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAkB,CAAC,CAAC,CAAA;IAClJ,OAAO,KAAK,CAAA;AACd,CAAC","sourcesContent":["import { currencyDigits } from '@things-factory/invoice'\nimport { Domain, Refusal } from '@things-factory/shell'\nimport { EntityManager, In } from 'typeorm'\n\nimport { SalesProduct } from './sales-product'\nimport { SalesProductKind, unitOf } from './sales-product-rules'\nimport { SalesProductPatch } from './sales-product-type'\n\nconst TEXT = ['grade', 'spec', 'description'] as const\n\n/** A patch as the row it writes: words trimmed, the unit the kind's when blank, the price and currency checked. */\nfunction rowOf(patch: SalesProductPatch, old?: SalesProduct): Partial<SalesProduct> {\n const row: Record<string, any> = {}\n if (patch.code !== undefined || !old) {\n const code = String(patch.code ?? '').trim()\n if (!code) throw new Refusal('sales-product-code-required', {}, 'Give the product a code')\n row.code = code\n }\n if (patch.name !== undefined || !old) {\n const name = String(patch.name ?? '').trim()\n if (!name) throw new Refusal('sales-product-name-required', {}, 'Name the product')\n row.name = name\n }\n const kind = patch.kind ?? old?.kind ?? SalesProductKind.GOODS\n row.kind = kind\n for (const field of TEXT) if (patch[field] !== undefined) row[field] = String(patch[field] ?? '').trim() || null\n /* A blank unit is the kind's - also when the kind changes and the unit was the old kind's own. */\n if (patch.unit !== undefined || !old) row.unit = unitOf(kind, patch.unit)\n else if (patch.kind !== undefined) row.unit = old.unit === unitOf(old.kind) ? unitOf(kind) : old.unit\n if (patch.currency !== undefined || !old) {\n row.currency = (String(patch.currency ?? '').trim() || 'KRW').toUpperCase()\n currencyDigits(row.currency)\n }\n if (patch.unitPrice !== undefined || !old) {\n const price = patch.unitPrice ?? 0\n if (!Number.isFinite(price) || price < 0) throw new Refusal('sales-product-price-invalid', {}, 'The price is zero or more')\n /* A quotation line takes a price to four decimals (invoice's document-amounts) - a finer one would be refused there. */\n if (Math.abs(price * 10_000 - Math.round(price * 10_000)) > 1e-6)\n throw new Refusal('sales-product-price-decimals', { value: String(price), max: 4 }, 'unitPrice: at most four decimals')\n row.unitPrice = price\n }\n if (patch.taxKind !== undefined || !old) row.taxKind = patch.taxKind ?? 'TAXABLE'\n if (patch.active !== undefined || !old) row.active = patch.active !== false\n return row\n}\n\n/**\n * Apply grid rows to a domain's products: `+` (or no id) registers, `M` changes, `-` deletes (softly). A code is the\n * domain's once among the products not deleted. Nothing names a product - a quotation line copies what it says - so a\n * product is deleted freely.\n */\nexport async function saveSalesProducts(tx: EntityManager, domain: Domain, user: any, patches: SalesProductPatch[]) {\n const repository = tx.getRepository(SalesProduct)\n const deletes = patches.filter(one => one.cuFlag === '-' && one.id).map(one => one.id!)\n const changes = patches.filter(one => one.cuFlag !== '-')\n\n if (deletes.length) {\n const owned = await repository.find({ where: { domain: { id: domain.id }, id: In(deletes) }, select: ['id'] })\n if (owned.length !== deletes.length) throw new Refusal('sales-product-not-found', {}, 'A product was not found - reload and try again')\n await repository.softDelete({ id: In(deletes) })\n }\n\n const rows: Partial<SalesProduct>[] = []\n for (const patch of changes) {\n const old = patch.id && patch.cuFlag !== '+' ? await repository.findOne({ where: { domain: { id: domain.id }, id: patch.id } }) : undefined\n if (patch.id && patch.cuFlag !== '+' && !old) throw new Refusal('sales-product-not-found', {}, 'A product was not found - reload and try again')\n rows.push({ ...(old ?? {}), ...rowOf(patch, old ?? undefined) })\n }\n\n /* One code, one product: against the rows kept and against one another in the batch. */\n const codes = rows.map(row => row.code!)\n const taken = codes.length ? await repository.find({ where: { domain: { id: domain.id }, code: In(codes) }, select: ['id', 'code'] }) : []\n for (const [index, row] of rows.entries()) {\n const clash = taken.find(one => one.code === row.code && one.id !== row.id) || rows.find((other, at) => at < index && other.code === row.code)\n if (clash) throw new Refusal('sales-product-code-duplicate', { code: row.code }, `Product code ${row.code} is already in use`)\n }\n\n const saved: SalesProduct[] = []\n for (const row of rows) saved.push(await repository.save({ ...row, domain, ...(row.id ? {} : { creator: user }), updater: user } as SalesProduct))\n return saved\n}\n"]}
|