@eve-line-sales-extension/core 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +37 -2
- package/dist/dates.d.ts +20 -0
- package/dist/dates.js +54 -0
- package/dist/demo-details.d.ts +23 -0
- package/dist/demo-details.js +61 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/salesforce.d.ts +71 -0
- package/dist/salesforce.js +141 -0
- package/package.json +51 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 M Intelligence
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,38 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @eve-line-sales-extension/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The demo-booking rules on their own, with no LINE or eve dependency. [`@eve-line-sales-extension/eve`](https://www.npmjs.com/package/@eve-line-sales-extension/eve) uses them.
|
|
4
|
+
|
|
5
|
+
- **Dates:** booking runs on Bangkok time.
|
|
6
|
+
- `bangkokNow`, `today` and `addDays`
|
|
7
|
+
- `isBookableDay` (from tomorrow to 90 days ahead)
|
|
8
|
+
- `thaiDate`, which writes "ศุกร์ 20 พ.ย. 69 10:00 น."
|
|
9
|
+
- **Demo requests:**
|
|
10
|
+
- `demoDetailsSchema` (zod);
|
|
11
|
+
- `normalizeDemoDetails`, which trims the details and requires a phone number or an email. It also checks the email and that the preferred day can still be booked.
|
|
12
|
+
- **Salesforce:**
|
|
13
|
+
- `salesforceConfig`, `createLead`, `updateDemoFields`, `leadUrl` and `SalesforceError`.
|
|
14
|
+
- The client signs in with the client credentials flow of an External Client App and signs in again once when the token expires.
|
|
15
|
+
- A repeated `Demo_Reference__c` counts as already saved, so a retried request never creates a second Lead.
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @eve-line-sales-extension/core
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { createLead, salesforceConfig } from "@eve-line-sales-extension/core";
|
|
23
|
+
|
|
24
|
+
const org = salesforceConfig({
|
|
25
|
+
loginUrl: process.env.SALESFORCE_LOGIN_URL,
|
|
26
|
+
clientId: process.env.SALESFORCE_CLIENT_ID,
|
|
27
|
+
clientSecret: process.env.SALESFORCE_CLIENT_SECRET,
|
|
28
|
+
});
|
|
29
|
+
if (org) {
|
|
30
|
+
await createLead(org, { Company: "Acme", LastName: "Jaidee", Demo_Reference__c: "DEMO-ABC123" });
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Source and tests are in the [repository](https://github.com/mintelligence-dev/eve-line-sales-extension).
|
|
35
|
+
|
|
36
|
+
## License
|
|
37
|
+
|
|
38
|
+
[MIT](LICENSE) © 2026 M Intelligence
|
package/dist/dates.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dates for demo bookings. LINE's pickers speak local wall-clock time without
|
|
3
|
+
* a zone; bookings run on Bangkok time.
|
|
4
|
+
*/
|
|
5
|
+
export declare const TIME_ZONE = "Asia/Bangkok";
|
|
6
|
+
/** How far ahead the customer and sales can pick a day. */
|
|
7
|
+
export declare const PICKER_DAYS = 90;
|
|
8
|
+
export declare const DATE_PATTERN: RegExp;
|
|
9
|
+
export declare const DATETIME_PATTERN: RegExp;
|
|
10
|
+
/** Now in Bangkok as YYYY-MM-DDTHH:mm. */
|
|
11
|
+
export declare function bangkokNow(): string;
|
|
12
|
+
/** A Bangkok calendar day plus some days, as YYYY-MM-DD. */
|
|
13
|
+
export declare function addDays(date: string, days: number): string;
|
|
14
|
+
export declare const today: () => string;
|
|
15
|
+
/** A picker value as an instant: Bangkok wall-clock time. */
|
|
16
|
+
export declare const instant: (value: string) => Date;
|
|
17
|
+
/** "ศุกร์ 20 พ.ย. 69" for a day, plus "10:00 น." when there is a time. */
|
|
18
|
+
export declare function thaiDate(value: string): string;
|
|
19
|
+
/** A day the customer may still choose: from tomorrow, within the window. */
|
|
20
|
+
export declare function isBookableDay(value: string): boolean;
|
package/dist/dates.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dates for demo bookings. LINE's pickers speak local wall-clock time without
|
|
3
|
+
* a zone; bookings run on Bangkok time.
|
|
4
|
+
*/
|
|
5
|
+
export const TIME_ZONE = "Asia/Bangkok";
|
|
6
|
+
/** How far ahead the customer and sales can pick a day. */
|
|
7
|
+
export const PICKER_DAYS = 90;
|
|
8
|
+
export const DATE_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
|
|
9
|
+
export const DATETIME_PATTERN = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}$/;
|
|
10
|
+
/** Now in Bangkok as YYYY-MM-DDTHH:mm. */
|
|
11
|
+
export function bangkokNow() {
|
|
12
|
+
const parts = Object.fromEntries(new Intl.DateTimeFormat("en-CA", {
|
|
13
|
+
day: "2-digit",
|
|
14
|
+
hour: "2-digit",
|
|
15
|
+
hourCycle: "h23",
|
|
16
|
+
minute: "2-digit",
|
|
17
|
+
month: "2-digit",
|
|
18
|
+
timeZone: TIME_ZONE,
|
|
19
|
+
year: "numeric",
|
|
20
|
+
})
|
|
21
|
+
.formatToParts(new Date())
|
|
22
|
+
.map((part) => [part.type, part.value]));
|
|
23
|
+
return `${parts.year}-${parts.month}-${parts.day}T${parts.hour}:${parts.minute}`;
|
|
24
|
+
}
|
|
25
|
+
/** A Bangkok calendar day plus some days, as YYYY-MM-DD. */
|
|
26
|
+
export function addDays(date, days) {
|
|
27
|
+
const [year, month, day] = date.split("-").map(Number);
|
|
28
|
+
return new Date(Date.UTC(year ?? 0, (month ?? 1) - 1, (day ?? 1) + days)).toISOString().slice(0, 10);
|
|
29
|
+
}
|
|
30
|
+
export const today = () => bangkokNow().slice(0, 10);
|
|
31
|
+
/** A picker value as an instant: Bangkok wall-clock time. */
|
|
32
|
+
export const instant = (value) => new Date(DATE_PATTERN.test(value) ? `${value}T00:00:00+07:00` : `${value}:00+07:00`);
|
|
33
|
+
const DAY_FORMAT = new Intl.DateTimeFormat("th-TH", {
|
|
34
|
+
day: "numeric",
|
|
35
|
+
month: "short",
|
|
36
|
+
timeZone: TIME_ZONE,
|
|
37
|
+
weekday: "short",
|
|
38
|
+
year: "2-digit",
|
|
39
|
+
});
|
|
40
|
+
const TIME_FORMAT = new Intl.DateTimeFormat("th-TH", {
|
|
41
|
+
hour: "2-digit",
|
|
42
|
+
hourCycle: "h23",
|
|
43
|
+
minute: "2-digit",
|
|
44
|
+
timeZone: TIME_ZONE,
|
|
45
|
+
});
|
|
46
|
+
/** "ศุกร์ 20 พ.ย. 69" for a day, plus "10:00 น." when there is a time. */
|
|
47
|
+
export function thaiDate(value) {
|
|
48
|
+
const day = DAY_FORMAT.format(instant(value));
|
|
49
|
+
return DATETIME_PATTERN.test(value) ? `${day} ${TIME_FORMAT.format(instant(value))} น.` : day;
|
|
50
|
+
}
|
|
51
|
+
/** A day the customer may still choose: from tomorrow, within the window. */
|
|
52
|
+
export function isBookableDay(value) {
|
|
53
|
+
return DATE_PATTERN.test(value) && value > today() && value <= addDays(today(), PICKER_DAYS);
|
|
54
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a demo request holds, shared by the tool that shows the customer a
|
|
3
|
+
* summary to confirm and the tool that saves the request, so both check the
|
|
4
|
+
* details the same way.
|
|
5
|
+
*/
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
export declare const demoDetailsSchema: z.ZodObject<{
|
|
8
|
+
company: z.ZodString;
|
|
9
|
+
email: z.ZodOptional<z.ZodString>;
|
|
10
|
+
interest: z.ZodString;
|
|
11
|
+
name: z.ZodString;
|
|
12
|
+
note: z.ZodOptional<z.ZodString>;
|
|
13
|
+
phone: z.ZodOptional<z.ZodString>;
|
|
14
|
+
preferredDate: z.ZodOptional<z.ZodString>;
|
|
15
|
+
preferredTime: z.ZodOptional<z.ZodString>;
|
|
16
|
+
}, z.core.$strip>;
|
|
17
|
+
export type DemoDetails = z.infer<typeof demoDetailsSchema>;
|
|
18
|
+
/**
|
|
19
|
+
* Trim the details and check what the model cannot be trusted to: a way to
|
|
20
|
+
* reach the customer, a plausible email, and a day that can still be booked.
|
|
21
|
+
* Each error tells the model what to ask the customer.
|
|
22
|
+
*/
|
|
23
|
+
export declare function normalizeDemoDetails(input: DemoDetails): DemoDetails;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a demo request holds, shared by the tool that shows the customer a
|
|
3
|
+
* summary to confirm and the tool that saves the request, so both check the
|
|
4
|
+
* details the same way.
|
|
5
|
+
*/
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
import { isBookableDay } from "./dates.js";
|
|
8
|
+
const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
9
|
+
export const demoDetailsSchema = z.object({
|
|
10
|
+
company: z.string().min(1).describe("Company name"),
|
|
11
|
+
email: z.string().optional().describe("Email, if the customer gave one"),
|
|
12
|
+
interest: z
|
|
13
|
+
.string()
|
|
14
|
+
.min(1)
|
|
15
|
+
.describe("What they want to see, for example Sales solution or a problem they described"),
|
|
16
|
+
name: z
|
|
17
|
+
.string()
|
|
18
|
+
.min(1)
|
|
19
|
+
.describe("Contact person's name, first name then surname, without a title such as คุณ or Mr."),
|
|
20
|
+
note: z.string().optional().describe("Anything else the customer told you that sales should know"),
|
|
21
|
+
phone: z.string().optional().describe("Phone number, if the customer gave one"),
|
|
22
|
+
preferredDate: z
|
|
23
|
+
.string()
|
|
24
|
+
.optional()
|
|
25
|
+
.describe("The customer's preferred day as YYYY-MM-DD, when they picked or named a specific day"),
|
|
26
|
+
preferredTime: z
|
|
27
|
+
.string()
|
|
28
|
+
.optional()
|
|
29
|
+
.describe("When in the day they would like the demo, in their own words, such as ช่วงเช้า"),
|
|
30
|
+
});
|
|
31
|
+
/**
|
|
32
|
+
* Trim the details and check what the model cannot be trusted to: a way to
|
|
33
|
+
* reach the customer, a plausible email, and a day that can still be booked.
|
|
34
|
+
* Each error tells the model what to ask the customer.
|
|
35
|
+
*/
|
|
36
|
+
export function normalizeDemoDetails(input) {
|
|
37
|
+
const email = input.email?.trim() || undefined;
|
|
38
|
+
const phone = input.phone?.trim() || undefined;
|
|
39
|
+
if (!(phone || email)) {
|
|
40
|
+
throw new Error("Need a phone number or an email so sales can reach the customer. Ask for one, then call again.");
|
|
41
|
+
}
|
|
42
|
+
if (email && !EMAIL.test(email)) {
|
|
43
|
+
throw new Error(`"${email}" does not look like an email address. Ask the customer to check it, then call again.`);
|
|
44
|
+
}
|
|
45
|
+
const preferredDate = input.preferredDate?.trim() || undefined;
|
|
46
|
+
if (preferredDate && !isBookableDay(preferredDate)) {
|
|
47
|
+
throw new Error(`${preferredDate} is not a day that can be booked: it must be YYYY-MM-DD, from tomorrow and within 90 days. ` +
|
|
48
|
+
"Ask the customer for another day, or leave preferredDate out, then call again.");
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
...input,
|
|
52
|
+
company: input.company.trim(),
|
|
53
|
+
email,
|
|
54
|
+
interest: input.interest.trim(),
|
|
55
|
+
name: input.name.trim(),
|
|
56
|
+
note: input.note?.trim() || undefined,
|
|
57
|
+
phone,
|
|
58
|
+
preferredDate,
|
|
59
|
+
preferredTime: input.preferredTime?.trim() || undefined,
|
|
60
|
+
};
|
|
61
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Salesforce REST client for the writes a demo booking makes: a Lead per demo
|
|
3
|
+
* request, then the demo fields on that Lead as sales books the demo in the
|
|
4
|
+
* LINE group and the customer answers.
|
|
5
|
+
*
|
|
6
|
+
* It signs in with the OAuth 2.0 client credentials flow of an External
|
|
7
|
+
* Client App (or Connected App) in the company org. Salesforce runs every
|
|
8
|
+
* call as the app's "Run As" user, so that user needs permission to create
|
|
9
|
+
* and edit Leads and nothing more. `loginUrl` is the org's My Domain URL:
|
|
10
|
+
* the flow does not work on login.salesforce.com.
|
|
11
|
+
*
|
|
12
|
+
* The app, the demo fields on Lead and the permission sets are in the
|
|
13
|
+
* repository's `salesforce/` folder, ready to deploy to the org.
|
|
14
|
+
*/
|
|
15
|
+
export interface SalesforceConfig {
|
|
16
|
+
clientId: string;
|
|
17
|
+
clientSecret: string;
|
|
18
|
+
loginUrl: string;
|
|
19
|
+
}
|
|
20
|
+
/** Where a demo booking stands, as the `Demo_Status__c` picklist spells it. */
|
|
21
|
+
export type DemoStatus = "Requested" | "Scheduled" | "Confirmed" | "Reschedule Requested";
|
|
22
|
+
/** The demo fields this agent keeps up to date on a Lead after creating it. */
|
|
23
|
+
export interface DemoLeadFields {
|
|
24
|
+
/** When sales booked the demo, as an ISO 8601 date-time. */
|
|
25
|
+
Demo_Date__c?: string;
|
|
26
|
+
/** The customer's preferred day, YYYY-MM-DD. */
|
|
27
|
+
Demo_Preferred_Date__c?: string;
|
|
28
|
+
Demo_Status__c?: DemoStatus;
|
|
29
|
+
}
|
|
30
|
+
/** The Lead fields this agent writes. `LastName` and `Company` are required. */
|
|
31
|
+
export interface LeadFields extends DemoLeadFields {
|
|
32
|
+
Company: string;
|
|
33
|
+
/** Unique in the org, so a second Lead with the same code is refused. */
|
|
34
|
+
Demo_Reference__c: string;
|
|
35
|
+
Description?: string;
|
|
36
|
+
Email?: string;
|
|
37
|
+
FirstName?: string;
|
|
38
|
+
LastName: string;
|
|
39
|
+
LeadSource?: string;
|
|
40
|
+
Phone?: string;
|
|
41
|
+
}
|
|
42
|
+
export declare class SalesforceError extends Error {
|
|
43
|
+
readonly code?: string;
|
|
44
|
+
readonly status: number;
|
|
45
|
+
constructor(message: string, status: number, code?: string);
|
|
46
|
+
}
|
|
47
|
+
/** A usable config from possibly blank values, or undefined unless all three are set. */
|
|
48
|
+
export declare function salesforceConfig(values: {
|
|
49
|
+
clientId?: string;
|
|
50
|
+
clientSecret?: string;
|
|
51
|
+
loginUrl?: string;
|
|
52
|
+
}): SalesforceConfig | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* Create the Lead for a reference, once. A replayed tool call sends the same
|
|
55
|
+
* reference, which the unique field turns away; that counts as saved, and
|
|
56
|
+
* `created` is false.
|
|
57
|
+
*/
|
|
58
|
+
export declare function createLead(config: SalesforceConfig, fields: LeadFields): Promise<{
|
|
59
|
+
created: boolean;
|
|
60
|
+
id?: string;
|
|
61
|
+
}>;
|
|
62
|
+
/**
|
|
63
|
+
* Write the demo fields on a Lead this agent created.
|
|
64
|
+
*
|
|
65
|
+
* The Run As user must be able to edit the Lead after assignment has given it
|
|
66
|
+
* to sales: with a Private Lead sharing model that takes a sharing rule (see
|
|
67
|
+
* README → Demo requests go to Salesforce).
|
|
68
|
+
*/
|
|
69
|
+
export declare function updateDemoFields(config: SalesforceConfig, leadId: string, fields: DemoLeadFields): Promise<void>;
|
|
70
|
+
/** The Lead's page in Lightning, on the org's My Domain. */
|
|
71
|
+
export declare function leadUrl(config: SalesforceConfig, leadId: string): string;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Salesforce REST client for the writes a demo booking makes: a Lead per demo
|
|
3
|
+
* request, then the demo fields on that Lead as sales books the demo in the
|
|
4
|
+
* LINE group and the customer answers.
|
|
5
|
+
*
|
|
6
|
+
* It signs in with the OAuth 2.0 client credentials flow of an External
|
|
7
|
+
* Client App (or Connected App) in the company org. Salesforce runs every
|
|
8
|
+
* call as the app's "Run As" user, so that user needs permission to create
|
|
9
|
+
* and edit Leads and nothing more. `loginUrl` is the org's My Domain URL:
|
|
10
|
+
* the flow does not work on login.salesforce.com.
|
|
11
|
+
*
|
|
12
|
+
* The app, the demo fields on Lead and the permission sets are in the
|
|
13
|
+
* repository's `salesforce/` folder, ready to deploy to the org.
|
|
14
|
+
*/
|
|
15
|
+
const API_VERSION = "v62.0";
|
|
16
|
+
const TIMEOUT_MS = 10_000;
|
|
17
|
+
export class SalesforceError extends Error {
|
|
18
|
+
code;
|
|
19
|
+
status;
|
|
20
|
+
constructor(message, status, code) {
|
|
21
|
+
super(message);
|
|
22
|
+
this.name = "SalesforceError";
|
|
23
|
+
this.status = status;
|
|
24
|
+
this.code = code;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/** A usable config from possibly blank values, or undefined unless all three are set. */
|
|
28
|
+
export function salesforceConfig(values) {
|
|
29
|
+
const loginUrl = values.loginUrl?.trim();
|
|
30
|
+
const clientId = values.clientId?.trim();
|
|
31
|
+
const clientSecret = values.clientSecret?.trim();
|
|
32
|
+
if (!(loginUrl && clientId && clientSecret)) {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
return { clientId, clientSecret, loginUrl: loginUrl.replace(/\/+$/, "") };
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* One token per function instance. A client credentials token carries no
|
|
39
|
+
* expiry: it lasts as long as the org's session timeout, so a 401 is the
|
|
40
|
+
* signal to sign in again.
|
|
41
|
+
*/
|
|
42
|
+
let cached;
|
|
43
|
+
async function signIn(config) {
|
|
44
|
+
const response = await fetch(`${config.loginUrl}/services/oauth2/token`, {
|
|
45
|
+
body: new URLSearchParams({
|
|
46
|
+
client_id: config.clientId,
|
|
47
|
+
client_secret: config.clientSecret,
|
|
48
|
+
grant_type: "client_credentials",
|
|
49
|
+
}),
|
|
50
|
+
method: "POST",
|
|
51
|
+
signal: AbortSignal.timeout(TIMEOUT_MS),
|
|
52
|
+
});
|
|
53
|
+
const body = (await response.json().catch(() => ({})));
|
|
54
|
+
if (!(response.ok && body.access_token && body.instance_url)) {
|
|
55
|
+
throw new SalesforceError(`Salesforce sign-in failed: ${body.error_description ?? body.error ?? `HTTP ${response.status}`}`, response.status, body.error);
|
|
56
|
+
}
|
|
57
|
+
return { accessToken: body.access_token, instanceUrl: body.instance_url };
|
|
58
|
+
}
|
|
59
|
+
function session(config, renew = false) {
|
|
60
|
+
if (cached && !renew) {
|
|
61
|
+
return cached;
|
|
62
|
+
}
|
|
63
|
+
const pending = signIn(config);
|
|
64
|
+
cached = pending;
|
|
65
|
+
// A failed sign-in must not stick: the next request tries again.
|
|
66
|
+
pending.catch(() => {
|
|
67
|
+
if (cached === pending) {
|
|
68
|
+
cached = undefined;
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
return pending;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Send one REST call, signing in again once if the cached token has outlived
|
|
75
|
+
* the org's session timeout. A 401 means nothing was written, so the retry is
|
|
76
|
+
* safe for any method.
|
|
77
|
+
*/
|
|
78
|
+
async function call(config, method, path, body, headers = {}) {
|
|
79
|
+
const send = ({ accessToken, instanceUrl }) => fetch(`${instanceUrl}/services/data/${API_VERSION}${path}`, {
|
|
80
|
+
body: JSON.stringify(body),
|
|
81
|
+
headers: {
|
|
82
|
+
Authorization: `Bearer ${accessToken}`,
|
|
83
|
+
"Content-Type": "application/json",
|
|
84
|
+
...headers,
|
|
85
|
+
},
|
|
86
|
+
method,
|
|
87
|
+
signal: AbortSignal.timeout(TIMEOUT_MS),
|
|
88
|
+
});
|
|
89
|
+
const response = await send(await session(config));
|
|
90
|
+
return response.status === 401 ? send(await session(config, true)) : response;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Create the Lead for a reference, once. A replayed tool call sends the same
|
|
94
|
+
* reference, which the unique field turns away; that counts as saved, and
|
|
95
|
+
* `created` is false.
|
|
96
|
+
*/
|
|
97
|
+
export async function createLead(config, fields) {
|
|
98
|
+
const response = await call(config, "POST", "/sobjects/Lead", fields, {
|
|
99
|
+
// Run the org's active Lead assignment rule so the Lead gets an owner.
|
|
100
|
+
"Sforce-Auto-Assign": "TRUE",
|
|
101
|
+
// A returning customer trips the standard Lead duplicate rule, whose
|
|
102
|
+
// alert would otherwise block an API insert. Save it; the rule still
|
|
103
|
+
// flags the match for sales.
|
|
104
|
+
"Sforce-Duplicate-Rule-Header": "allowSave=true",
|
|
105
|
+
});
|
|
106
|
+
const body = (await response.json().catch(() => undefined));
|
|
107
|
+
if (response.ok && body && !Array.isArray(body) && body.id) {
|
|
108
|
+
return { created: true, id: body.id };
|
|
109
|
+
}
|
|
110
|
+
const [error] = Array.isArray(body) ? body : [];
|
|
111
|
+
// The integration user may not be able to read the Lead once assignment
|
|
112
|
+
// has given it to sales, so the ID comes from the error text, not a query.
|
|
113
|
+
if (error?.errorCode === "DUPLICATE_VALUE" &&
|
|
114
|
+
error.message?.includes("Demo_Reference__c")) {
|
|
115
|
+
return {
|
|
116
|
+
created: false,
|
|
117
|
+
id: error.message.match(/record with id: (\w+)/)?.[1],
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
throw new SalesforceError(`Salesforce rejected the Lead: ${error?.message ?? `HTTP ${response.status}`}`, response.status, error?.errorCode);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Write the demo fields on a Lead this agent created.
|
|
124
|
+
*
|
|
125
|
+
* The Run As user must be able to edit the Lead after assignment has given it
|
|
126
|
+
* to sales: with a Private Lead sharing model that takes a sharing rule (see
|
|
127
|
+
* README → Demo requests go to Salesforce).
|
|
128
|
+
*/
|
|
129
|
+
export async function updateDemoFields(config, leadId, fields) {
|
|
130
|
+
const response = await call(config, "PATCH", `/sobjects/Lead/${encodeURIComponent(leadId)}`, fields);
|
|
131
|
+
if (response.ok) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const body = (await response.json().catch(() => undefined));
|
|
135
|
+
const [error] = Array.isArray(body) ? body : [];
|
|
136
|
+
throw new SalesforceError(`Salesforce rejected the Lead update: ${error?.message ?? `HTTP ${response.status}`}`, response.status, error?.errorCode);
|
|
137
|
+
}
|
|
138
|
+
/** The Lead's page in Lightning, on the org's My Domain. */
|
|
139
|
+
export function leadUrl(config, leadId) {
|
|
140
|
+
return `${config.loginUrl}/lightning/r/Lead/${encodeURIComponent(leadId)}/view`;
|
|
141
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eve-line-sales-extension/core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Demo booking rules without a framework: the demo request schema and checks, Bangkok booking dates, and a Salesforce client that creates and updates demo Leads.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"salesforce",
|
|
7
|
+
"lead",
|
|
8
|
+
"demo-booking",
|
|
9
|
+
"thai"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"author": "M Intelligence",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/mintelligence-dev/eve-line-sales-extension.git",
|
|
16
|
+
"directory": "packages/core"
|
|
17
|
+
},
|
|
18
|
+
"type": "module",
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"README.md",
|
|
23
|
+
"LICENSE"
|
|
24
|
+
],
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"default": "./dist/index.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsc -p tsconfig.build.json",
|
|
33
|
+
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
34
|
+
"prepublishOnly": "npm run build"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"zod": "4.5.4"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/node": "24.x",
|
|
41
|
+
"typescript": "7.0.2"
|
|
42
|
+
},
|
|
43
|
+
"engines": {
|
|
44
|
+
"node": ">=24"
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public"
|
|
48
|
+
},
|
|
49
|
+
"homepage": "https://github.com/mintelligence-dev/eve-line-sales-extension#readme",
|
|
50
|
+
"bugs": {
|
|
51
|
+
"url": "https://github.com/mintelligence-dev/eve-line-sales-extension/issues"
|
|
52
|
+
}
|
|
53
|
+
}
|