import type { JtMembership, ResolveJt } from "./resolve.js"; export interface MailJt extends ResolveJt { job(jobId: string): Promise<{ id: string; name: string; address: string | null } | null>; /** Пользователь JobTread (менеджер): имя, email, телефон. */ manager(membershipId: string): Promise<{ name: string; email: string | null; phone: string | null } | null>; /** Комментарий к проекту (история коммуникаций); возвращает id. */ comment(jobId: string, message: string): Promise; } /** Минимальный клиент JobTread (Pave) для писем подрядчикам: чтение членов команды и проектов, запись комментария. */ export class PaveMailJt implements MailJt { constructor(private o: { grantKey: string; endpoint?: string; fetchFn?: typeof fetch }) {} private async query(q: Record): Promise { const res = await (this.o.fetchFn ?? fetch)(this.o.endpoint ?? "https://api.jobtread.com/pave", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ query: { $: { grantKey: this.o.grantKey }, ...q } }), signal: AbortSignal.timeout(20_000), }); const text = await res.text(); if (!res.ok) throw new Error(`JobTread HTTP ${res.status}: ${text.slice(0, 300)}`); return JSON.parse(text) as T; } async memberships(ids: string[]): Promise { const out: JtMembership[] = []; for (const id of ids) { const r = await this.query<{ membership: { id: string; user?: { name?: string; emailAddress?: string | null } | null; account?: { id: string; name: string; customFieldValues?: { nodes?: { value?: string | null; customField?: { name?: string } }[] } } | null } | null }>({ membership: { $: { id }, id: {}, user: { name: {}, emailAddress: {} }, account: { id: {}, name: {}, customFieldValues: { nodes: { value: {}, customField: { name: {} } } } } }, }); const m = r.membership; if (!m) continue; const email = m.account?.customFieldValues?.nodes?.find((n) => n.customField?.name === "Email")?.value ?? null; out.push({ id: m.id, userName: m.user?.name ?? "", userEmail: m.user?.emailAddress ?? null, accountId: m.account?.id ?? null, accountName: m.account?.name ?? null, accountEmail: email }); } return out; } async job(jobId: string) { const r = await this.query<{ job: { id: string; name: string; location?: { address?: string | null } | null } | null }>({ job: { $: { id: jobId }, id: {}, name: {}, location: { address: {} } } }); return r.job ? { id: r.job.id, name: r.job.name, address: r.job.location?.address ?? null } : null; } async manager(membershipId: string) { const r = await this.query<{ membership: { user?: { name?: string; emailAddress?: string | null; phoneNumber?: string | null } | null } | null }>({ membership: { $: { id: membershipId }, user: { name: {}, emailAddress: {}, phoneNumber: {} } } }); const u = r.membership?.user; return u ? { name: u.name ?? "", email: u.emailAddress ?? null, phone: u.phoneNumber ?? null } : null; } async comment(jobId: string, message: string): Promise { const r = await this.query<{ createComment: { createdComment: { id: string } } }>({ createComment: { $: { targetId: jobId, targetType: "job", message }, createdComment: { id: {} } } }); return r.createComment.createdComment.id; } }