Release v0.1.4
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import type { ApiSettings } from "../../../types";
|
||||
import { listFiles, resolveFilePatterns, shareFilesWithCampaign, type ManagedFile } from "../../../api/files";
|
||||
import type { ApiSettings, FilesFileExplorerUiCapability, FilesManagedFile } from "@govoplan/core-webui";
|
||||
import { parseManagedAttachmentSource, type AttachmentBasePath } from "./attachments";
|
||||
import type { ImportedRecipientRow, RecipientImportPreview } from "./bulkImport";
|
||||
|
||||
@@ -10,17 +9,20 @@ export type RenderedImportPattern = {
|
||||
renderedPattern: string;
|
||||
};
|
||||
|
||||
export type ImportFileLinkCapability = Pick<Required<FilesFileExplorerUiCapability>, "listFiles" | "resolveFilePatterns" | "shareFilesWithTarget">;
|
||||
|
||||
export type ImportFileLinkResolution = {
|
||||
basePath: AttachmentBasePath;
|
||||
patterns: RenderedImportPattern[];
|
||||
matchedPatterns: { pattern: RenderedImportPattern; matches: ManagedFile[] }[];
|
||||
matchedPatterns: { pattern: RenderedImportPattern; matches: FilesManagedFile[] }[];
|
||||
unmatchedPatterns: RenderedImportPattern[];
|
||||
files: ManagedFile[];
|
||||
linkedFiles: ManagedFile[];
|
||||
linkableFiles: ManagedFile[];
|
||||
files: FilesManagedFile[];
|
||||
linkedFiles: FilesManagedFile[];
|
||||
linkableFiles: FilesManagedFile[];
|
||||
};
|
||||
|
||||
export async function resolveImportedAttachmentLinks(
|
||||
filesCapability: ImportFileLinkCapability,
|
||||
settings: ApiSettings,
|
||||
campaignId: string,
|
||||
basePath: AttachmentBasePath,
|
||||
@@ -38,14 +40,14 @@ export async function resolveImportedAttachmentLinks(
|
||||
|
||||
const root = normalizedPathPrefix(basePath.path);
|
||||
const [resolved, visibleFiles] = await Promise.all([
|
||||
resolveFilePatterns(settings, {
|
||||
filesCapability.resolveFilePatterns(settings, {
|
||||
patterns: patterns.map((item) => item.renderedPattern),
|
||||
owner_type: parsedSource.ownerType,
|
||||
owner_id: parsedSource.ownerId,
|
||||
path_prefix: root || undefined,
|
||||
include_unmatched: false
|
||||
}),
|
||||
listFiles(settings, {
|
||||
filesCapability.listFiles(settings, {
|
||||
owner_type: parsedSource.ownerType,
|
||||
owner_id: parsedSource.ownerId,
|
||||
path_prefix: root || undefined
|
||||
@@ -53,7 +55,7 @@ export async function resolveImportedAttachmentLinks(
|
||||
]);
|
||||
|
||||
const visibleById = new Map(visibleFiles.files.map((file) => [file.id, file]));
|
||||
const fileById = new Map<string, ManagedFile>();
|
||||
const fileById = new Map<string, FilesManagedFile>();
|
||||
const matchedPatterns = patterns.map((pattern, index) => {
|
||||
const matches = (resolved.patterns[index]?.matches ?? [])
|
||||
.map((file) => visibleById.get(file.id) ?? file);
|
||||
@@ -75,10 +77,10 @@ export async function resolveImportedAttachmentLinks(
|
||||
};
|
||||
}
|
||||
|
||||
export async function bulkLinkFilesToCampaign(settings: ApiSettings, campaignId: string, files: ManagedFile[]): Promise<number> {
|
||||
export async function bulkLinkFilesToCampaign(filesCapability: ImportFileLinkCapability, settings: ApiSettings, campaignId: string, files: FilesManagedFile[]): Promise<number> {
|
||||
const uniqueIds = [...new Set(files.map((file) => file.id).filter(Boolean))];
|
||||
if (uniqueIds.length === 0) return 0;
|
||||
const response = await shareFilesWithCampaign(settings, uniqueIds, campaignId);
|
||||
const response = await filesCapability.shareFilesWithTarget(settings, uniqueIds, { type: "campaign", id: campaignId, label: "campaign" });
|
||||
return response.shared_count;
|
||||
}
|
||||
|
||||
@@ -97,7 +99,7 @@ export function renderedImportPatterns(preview: RecipientImportPreview): Rendere
|
||||
return [...byKey.values()];
|
||||
}
|
||||
|
||||
export function isSharedWithCampaign(file: ManagedFile, campaignId: string): boolean {
|
||||
export function isSharedWithCampaign(file: FilesManagedFile, campaignId: string): boolean {
|
||||
return Boolean(file.shares?.some((share) => share.target_type === "campaign" && share.target_id === campaignId && !share.revoked_at));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user