mirror of
https://github.com/christianvidalwolf-prog/Craze-Data-check.git
synced 2026-08-03 19:35:24 +02:00
fix: commit all BC sync files missing from repo
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
const BC_PROXY_URL = '/api/bc-proxy';
|
||||
const BC_SYNC_PREVIEW_URL = '/api/bc-sync-preview';
|
||||
const BC_SYNC_APPLY_URL = '/api/bc-sync-apply';
|
||||
const BC_EXPORT_URL = '/api/bc-export';
|
||||
|
||||
export interface BCUpdateResult {
|
||||
@@ -6,12 +7,60 @@ export interface BCUpdateResult {
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export function isPreviewTokenMismatchError(error?: string): boolean {
|
||||
return Boolean(error && error.toLowerCase().includes('preview token mismatch'));
|
||||
}
|
||||
|
||||
export interface BCDownloadResult {
|
||||
success: boolean;
|
||||
error?: string;
|
||||
filename?: string;
|
||||
}
|
||||
|
||||
export interface BCFieldChange {
|
||||
sourceLabel: string;
|
||||
sourceIndex: number | null;
|
||||
targetField: string;
|
||||
before: any;
|
||||
after: any;
|
||||
changed: boolean;
|
||||
}
|
||||
|
||||
export interface BCSyncPreviewSection {
|
||||
type: 'items' | 'itemUnitsOfMeasure';
|
||||
desired: Record<string, any>;
|
||||
current: Record<string, any> | null;
|
||||
changes: BCFieldChange[];
|
||||
changedFields: string[];
|
||||
writeConfigured: boolean;
|
||||
writeMethod: string | null;
|
||||
writeUrlTemplate: string | null;
|
||||
writeBodyTemplate: string | null;
|
||||
canApply: boolean;
|
||||
}
|
||||
|
||||
export interface BCSyncPreviewResult {
|
||||
success: boolean;
|
||||
articleNo: string;
|
||||
items: BCSyncPreviewSection;
|
||||
itemUnitsOfMeasure: BCSyncPreviewSection;
|
||||
hasChanges: boolean;
|
||||
previewToken: string;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export interface BCSyncApplyResult {
|
||||
success: boolean;
|
||||
articleNo: string;
|
||||
previewToken?: string;
|
||||
preview?: BCSyncPreviewResult;
|
||||
results?: {
|
||||
items: { applied: boolean; reason?: string; url?: string };
|
||||
itemUnitsOfMeasure: { applied: boolean; reason?: string; url?: string };
|
||||
};
|
||||
error?: string;
|
||||
}
|
||||
|
||||
async function readResponsePayload(res: Response): Promise<{ data: any; rawText: string }> {
|
||||
const rawText = await res.text();
|
||||
if (!rawText.trim()) {
|
||||
@@ -27,7 +76,7 @@ async function readResponsePayload(res: Response): Promise<{ data: any; rawText:
|
||||
|
||||
export async function updateCpnpNoInBC(articleNo: string, cpnpNo: string): Promise<BCUpdateResult> {
|
||||
try {
|
||||
const res = await fetch(BC_PROXY_URL, {
|
||||
const res = await fetch(BC_SYNC_APPLY_URL, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ articleNo, cpnpNo }),
|
||||
@@ -48,6 +97,52 @@ export async function updateCpnpNoInBC(articleNo: string, cpnpNo: string): Promi
|
||||
}
|
||||
}
|
||||
|
||||
export async function previewBusinessCentralSync(headers: string[], row: any[]): Promise<BCSyncPreviewResult> {
|
||||
try {
|
||||
const res = await fetch(BC_SYNC_PREVIEW_URL, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ headers, row }),
|
||||
});
|
||||
|
||||
const { data, rawText } = await readResponsePayload(res);
|
||||
if (!res.ok || !data?.success) {
|
||||
const error =
|
||||
data?.error ||
|
||||
(typeof data === 'string' && data.trim()) ||
|
||||
rawText ||
|
||||
`HTTP ${res.status}`;
|
||||
return { success: false, error } as BCSyncPreviewResult;
|
||||
}
|
||||
return data as BCSyncPreviewResult;
|
||||
} catch (err: any) {
|
||||
return { success: false, error: err.message } as BCSyncPreviewResult;
|
||||
}
|
||||
}
|
||||
|
||||
export async function applyBusinessCentralSync(headers: string[], row: any[], previewToken?: string): Promise<BCSyncApplyResult> {
|
||||
try {
|
||||
const res = await fetch(BC_SYNC_APPLY_URL, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ headers, row, previewToken }),
|
||||
});
|
||||
|
||||
const { data, rawText } = await readResponsePayload(res);
|
||||
if (!res.ok || !data?.success) {
|
||||
const error =
|
||||
data?.error ||
|
||||
(typeof data === 'string' && data.trim()) ||
|
||||
rawText ||
|
||||
`HTTP ${res.status}`;
|
||||
return { success: false, error } as BCSyncApplyResult;
|
||||
}
|
||||
return data as BCSyncApplyResult;
|
||||
} catch (err: any) {
|
||||
return { success: false, error: err.message } as BCSyncApplyResult;
|
||||
}
|
||||
}
|
||||
|
||||
function getFilenameFromDisposition(contentDisposition: string | null): string | null {
|
||||
if (!contentDisposition) return null;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user