From 396dd3e7f4d72f04dbc711a39bd38562fe5a5f3e Mon Sep 17 00:00:00 2001 From: Andrey Sobolev Date: Thu, 5 Jan 2023 11:45:38 +0700 Subject: [PATCH] Minor fixes (#2490) Signed-off-by: Andrey Sobolev --- models/bitrix/src/index.ts | 2 +- .../src/components/AttachmentRefInput.svelte | 2 +- .../src/components/AttachmentStyledBox.svelte | 2 +- .../src/components/Photos.svelte | 2 +- plugins/attachment-resources/src/utils.ts | 19 +--- plugins/attachment/src/index.ts | 4 +- plugins/bitrix-resources/src/client.ts | 2 +- .../src/components/AttributeMapper.svelte | 6 ++ .../src/components/BitrixFieldLookup.svelte | 2 +- .../components/CreateMappingAttribute.svelte | 5 +- .../components/FieldMappingPresenter.svelte | 3 + .../FieldMappingSynchronizer.svelte | 20 ++-- .../mappings/DownloadAttachmentMapping.svelte | 100 ++++++++++++++++++ .../DownloadAttachmentPresenter.svelte | 37 +++++++ plugins/bitrix/src/index.ts | 14 ++- .../src/components/NewMessage.svelte | 2 +- plugins/login-resources/src/utils.ts | 12 ++- .../src/components/CreateCandidate.svelte | 5 +- server/core/src/utils.ts | 0 server/front/src/index.ts | 20 ---- 20 files changed, 192 insertions(+), 67 deletions(-) create mode 100644 plugins/bitrix-resources/src/components/mappings/DownloadAttachmentMapping.svelte create mode 100644 plugins/bitrix-resources/src/components/mappings/DownloadAttachmentPresenter.svelte delete mode 100644 server/core/src/utils.ts diff --git a/models/bitrix/src/index.ts b/models/bitrix/src/index.ts index 9a29c0c6fb..a3055767df 100644 --- a/models/bitrix/src/index.ts +++ b/models/bitrix/src/index.ts @@ -87,7 +87,7 @@ export function createModel (builder: Builder): void { }, label: bitrix.string.BitrixImport, icon: bitrix.icon.Bitrix, - input: 'any', + input: 'none', category: view.category.General, target: core.class.Doc, context: { mode: ['workbench', 'browser', 'editor', 'panel', 'popup'], group: 'create' } diff --git a/plugins/attachment-resources/src/components/AttachmentRefInput.svelte b/plugins/attachment-resources/src/components/AttachmentRefInput.svelte index bf0836837f..9fc961bafd 100644 --- a/plugins/attachment-resources/src/components/AttachmentRefInput.svelte +++ b/plugins/attachment-resources/src/components/AttachmentRefInput.svelte @@ -87,7 +87,7 @@ async function createAttachment (file: File) { try { - const uuid = await uploadFile(file, { space, attachedTo: objectId }) + const uuid = await uploadFile(file) const _id: Ref = generateId() attachments.set(_id, { _id, diff --git a/plugins/attachment-resources/src/components/AttachmentStyledBox.svelte b/plugins/attachment-resources/src/components/AttachmentStyledBox.svelte index 2cd4e0813e..0f247f0330 100644 --- a/plugins/attachment-resources/src/components/AttachmentStyledBox.svelte +++ b/plugins/attachment-resources/src/components/AttachmentStyledBox.svelte @@ -112,7 +112,7 @@ async function createAttachment (file: File) { if (space === undefined || objectId === undefined || _class === undefined) return try { - const uuid = await uploadFile(file, { space, attachedTo: objectId }) + const uuid = await uploadFile(file) const _id: Ref = generateId() attachments.set(_id, { _id, diff --git a/plugins/attachment-resources/src/components/Photos.svelte b/plugins/attachment-resources/src/components/Photos.svelte index 1ee723ce45..acd84515a0 100644 --- a/plugins/attachment-resources/src/components/Photos.svelte +++ b/plugins/attachment-resources/src/components/Photos.svelte @@ -47,7 +47,7 @@ if (!file.type.startsWith('image/')) return loading++ try { - const uuid = await uploadFile(file, { space, attachedTo: objectId }) + const uuid = await uploadFile(file) client.addCollection(attachment.class.Photo, space, objectId, _class, 'photos', { name: file.name, file: uuid, diff --git a/plugins/attachment-resources/src/utils.ts b/plugins/attachment-resources/src/utils.ts index e725d9f0be..4fb2e255f1 100644 --- a/plugins/attachment-resources/src/utils.ts +++ b/plugins/attachment-resources/src/utils.ts @@ -21,7 +21,7 @@ import { getMetadata, setPlatformStatus, unknownError } from '@hcengineering/pla import attachment from './plugin' -export async function uploadFile (file: File, opts?: { space: Ref, attachedTo: Ref }): Promise { +export async function uploadFile (file: File): Promise { const uploadUrl = getMetadata(login.metadata.UploadUrl) if (uploadUrl === undefined) { @@ -31,20 +31,7 @@ export async function uploadFile (file: File, opts?: { space: Ref, attach const data = new FormData() data.append('file', file) - const params = - opts !== undefined - ? [ - ['space', opts.space], - ['attachedTo', opts.attachedTo] - ] - .filter((x): x is [string, Ref] => x[1] !== undefined) - .map(([name, value]) => `${name}=${value}`) - .join('&') - : '' - const suffix = params === '' ? params : `?${params}` - - const url = `${uploadUrl}${suffix}` - const resp = await fetch(url, { + const resp = await fetch(uploadUrl, { method: 'POST', headers: { Authorization: 'Bearer ' + (getMetadata(login.metadata.LoginToken) as string) @@ -87,7 +74,7 @@ export async function createAttachments ( for (let index = 0; index < list.length; index++) { const file = list.item(index) if (file !== null) { - const uuid = await uploadFile(file, { space, attachedTo: objectId }) + const uuid = await uploadFile(file) await client.addCollection(attachmentClass, space, objectId, objectClass, 'attachments', { ...extraData, name: file.name, diff --git a/plugins/attachment/src/index.ts b/plugins/attachment/src/index.ts index 7123865e67..7386a9c96e 100644 --- a/plugins/attachment/src/index.ts +++ b/plugins/attachment/src/index.ts @@ -14,7 +14,7 @@ // limitations under the License. // -import type { AttachedDoc, Class, Doc, Ref, Space } from '@hcengineering/core' +import type { AttachedDoc, Class, Ref } from '@hcengineering/core' import type { Asset, Plugin } from '@hcengineering/platform' import { IntlString, plugin, Resource } from '@hcengineering/platform' import type { Preference } from '@hcengineering/preference' @@ -66,7 +66,7 @@ export default plugin(attachmentId, { SavedAttachments: '' as Ref> }, helper: { - UploadFile: '' as Resource<(file: File, opts?: { space: Ref, attachedTo: Ref }) => Promise>, + UploadFile: '' as Resource<(file: File) => Promise>, DeleteFile: '' as Resource<(id: string) => Promise> }, string: { diff --git a/plugins/bitrix-resources/src/client.ts b/plugins/bitrix-resources/src/client.ts index 822715c0e5..366b595907 100644 --- a/plugins/bitrix-resources/src/client.ts +++ b/plugins/bitrix-resources/src/client.ts @@ -19,7 +19,7 @@ export class BitrixClient { await fetch(`${this.url}/${method}${query}`, { method: 'get', headers: { - 'user-agent': 'anticrm' + 'Content-Type': 'application/json' } }) ).json() diff --git a/plugins/bitrix-resources/src/components/AttributeMapper.svelte b/plugins/bitrix-resources/src/components/AttributeMapper.svelte index 81a642843e..5ad0b58f2b 100644 --- a/plugins/bitrix-resources/src/components/AttributeMapper.svelte +++ b/plugins/bitrix-resources/src/components/AttributeMapper.svelte @@ -56,6 +56,12 @@ action: (_: any, evt: MouseEvent) => { addMapping(evt, MappingOperation.CreateChannel) } + }, + { + label: getEmbeddedLabel('Add Download Attachment mapping'), + action: (_: any, evt: MouseEvent) => { + addMapping(evt, MappingOperation.DownloadAttachment) + } } ] as Action[] diff --git a/plugins/bitrix-resources/src/components/BitrixFieldLookup.svelte b/plugins/bitrix-resources/src/components/BitrixFieldLookup.svelte index 9b4e7c2c59..7768f533ec 100644 --- a/plugins/bitrix-resources/src/components/BitrixFieldLookup.svelte +++ b/plugins/bitrix-resources/src/components/BitrixFieldLookup.svelte @@ -29,7 +29,7 @@ let items: any[] = [] function loadItems (order: boolean): void { bitrixClient - .call(mapping.type + '.list', { select: ['*', 'UF_*'], order: { ID: order ? 'ASC' : 'DSC' } }) + .call(mapping.type + '.list', { select: ['*', 'UF_*', 'EMAIL', 'IM'], order: { ID: order ? 'ASC' : 'DSC' } }) .then((res) => { items = res.result }) diff --git a/plugins/bitrix-resources/src/components/CreateMappingAttribute.svelte b/plugins/bitrix-resources/src/components/CreateMappingAttribute.svelte index 7d877c7a8f..932d2c6cb4 100644 --- a/plugins/bitrix-resources/src/components/CreateMappingAttribute.svelte +++ b/plugins/bitrix-resources/src/components/CreateMappingAttribute.svelte @@ -7,6 +7,7 @@ import CopyMapping from './mappings/CopyMapping.svelte' import CreateChannelMapping from './mappings/CreateChannelMapping.svelte' import CreateTagMapping from './mappings/CreateTagMapping.svelte' + import DownloadAttachmentMapping from './mappings/DownloadAttachmentMapping.svelte' export let mapping: BitrixEntityMapping export let fields: Fields = {} @@ -16,7 +17,7 @@ $: _kind = kind ?? field?.operation.kind - let op: CopyMapping | CreateTagMapping | CreateChannelMapping + let op: CopyMapping | CreateTagMapping | CreateChannelMapping | DownloadAttachmentMapping async function save (): Promise { op.save() @@ -39,5 +40,7 @@ {:else if _kind === MappingOperation.CreateChannel} + {:else if _kind === MappingOperation.DownloadAttachment} + {/if} diff --git a/plugins/bitrix-resources/src/components/FieldMappingPresenter.svelte b/plugins/bitrix-resources/src/components/FieldMappingPresenter.svelte index e07bb4bf51..2c4fd054bd 100644 --- a/plugins/bitrix-resources/src/components/FieldMappingPresenter.svelte +++ b/plugins/bitrix-resources/src/components/FieldMappingPresenter.svelte @@ -6,6 +6,7 @@ import CopyMappingPresenter from './mappings/CopyMappingPresenter.svelte' import CreateChannelMappingPresenter from './mappings/CreateChannelMappingPresenter.svelte' import CreateTagMappingPresenter from './mappings/CreateTagMappingPresenter.svelte' + import DownloadAttachmentPresenter from './mappings/DownloadAttachmentPresenter.svelte' export let mapping: BitrixEntityMapping export let value: BitrixFieldMapping @@ -29,6 +30,8 @@ {:else if kind === MappingOperation.CreateChannel} + {:else if kind === MappingOperation.DownloadAttachment} + {/if} {/if} diff --git a/plugins/bitrix-resources/src/components/FieldMappingSynchronizer.svelte b/plugins/bitrix-resources/src/components/FieldMappingSynchronizer.svelte index 79763587b2..e14de729e8 100644 --- a/plugins/bitrix-resources/src/components/FieldMappingSynchronizer.svelte +++ b/plugins/bitrix-resources/src/components/FieldMappingSynchronizer.svelte @@ -252,8 +252,13 @@ let added = 0 while (added <= limit) { + const sel = ['*', 'UF_*'] + if (mapping.type === BitrixEntityType.Lead) { + sel.push('EMAIL') + sel.push('IM') + } const result = await bitrixClient.call(mapping.type + '.list', { - select: ['*', 'UF_*'], + select: sel, order: { ID: direction }, start: processed }) @@ -261,18 +266,11 @@ const extraDocs: Doc[] = [] const convertResults: ConvertResult[] = [] + const fields = mapping.$lookup?.fields as BitrixFieldMapping[] + for (const r of result.result) { // Convert documents. - const res = await convert( - client, - mapping, - space, - mapping.$lookup?.fields as BitrixFieldMapping[], - r, - extraDocs, - tagElements, - userList - ) + const res = await convert(client, mapping, space, fields, r, extraDocs, tagElements, userList) if (mapping.comments) { res.comments = bitrixClient .call(BitrixEntityType.Comment + '.list', { diff --git a/plugins/bitrix-resources/src/components/mappings/DownloadAttachmentMapping.svelte b/plugins/bitrix-resources/src/components/mappings/DownloadAttachmentMapping.svelte new file mode 100644 index 0000000000..21a158932d --- /dev/null +++ b/plugins/bitrix-resources/src/components/mappings/DownloadAttachmentMapping.svelte @@ -0,0 +1,100 @@ + + +
+ {#each downloadFields as p, i} +
+ + +
+
+
+ {/each} +
+
+
+ + diff --git a/plugins/bitrix-resources/src/components/mappings/DownloadAttachmentPresenter.svelte b/plugins/bitrix-resources/src/components/mappings/DownloadAttachmentPresenter.svelte new file mode 100644 index 0000000000..325ecc2b16 --- /dev/null +++ b/plugins/bitrix-resources/src/components/mappings/DownloadAttachmentPresenter.svelte @@ -0,0 +1,37 @@ + + +
+ {#each op.fields as p, i} +
+ {#if mapping.bitrixFields} + {p.field ? mapping.bitrixFields[p.field]?.formLabel ?? mapping.bitrixFields[p.field]?.title : p.field ?? ''} + {/if} +
+ {/each} +
+ + diff --git a/plugins/bitrix/src/index.ts b/plugins/bitrix/src/index.ts index 50143c0075..60a68a575f 100644 --- a/plugins/bitrix/src/index.ts +++ b/plugins/bitrix/src/index.ts @@ -97,7 +97,8 @@ export interface BitrixEntityMapping extends Doc { export enum MappingOperation { CopyValue, CreateTag, // Create tag - CreateChannel // Create channel + CreateChannel, // Create channel + DownloadAttachment } /** * @public @@ -149,6 +150,15 @@ export interface CreateChannelOperation { fields: ChannelFieldMapping[] } +/** + * @public + */ +export interface DownloadAttachmentOperation { + kind: MappingOperation.DownloadAttachment + + fields: { field: string }[] +} + /** * @public */ @@ -156,7 +166,7 @@ export interface BitrixFieldMapping extends AttachedDoc { ofClass: Ref> // Specify mixin if applicable attributeName: string - operation: CopyValueOperation | CreateTagOperation | CreateChannelOperation + operation: CopyValueOperation | CreateTagOperation | CreateChannelOperation | DownloadAttachmentOperation } /** diff --git a/plugins/gmail-resources/src/components/NewMessage.svelte b/plugins/gmail-resources/src/components/NewMessage.svelte index 43a2dac984..07a46c8037 100644 --- a/plugins/gmail-resources/src/components/NewMessage.svelte +++ b/plugins/gmail-resources/src/components/NewMessage.svelte @@ -90,7 +90,7 @@ async function createAttachment (file: File) { try { const uploadFile = await getResource(attachmentP.helper.UploadFile) - const uuid = await uploadFile(file, { space: plugin.space.Gmail, attachedTo: objectId }) + const uuid = await uploadFile(file) await client.addCollection( attachmentP.class.Attachment, plugin.space.Gmail, diff --git a/plugins/login-resources/src/utils.ts b/plugins/login-resources/src/utils.ts index 3dcceded45..73965e5ebf 100644 --- a/plugins/login-resources/src/utils.ts +++ b/plugins/login-resources/src/utils.ts @@ -294,11 +294,15 @@ export function navigateToWorkspace (workspace: string, loginInfo?: WorkspaceLog if (navigateUrl !== undefined) { const loc = JSON.parse(decodeURIComponent(navigateUrl)) as Location - const url = JSON.parse(decodeURIComponent(loc.query?.navigateUrl ?? '')) as Location - if (url.path[1] === workspace) { - navigate(url) + try { + const url = JSON.parse(decodeURIComponent(loc.query?.navigateUrl ?? '{}')) as Location + if (url.path[1] === workspace) { + navigate(url) - return + return + } + } catch (err: any) { + // Json parse error could be ignored } } navigate({ path: [workbenchId, workspace] }) diff --git a/plugins/recruit-resources/src/components/CreateCandidate.svelte b/plugins/recruit-resources/src/components/CreateCandidate.svelte index 82c2ef516e..cfbe87cf88 100644 --- a/plugins/recruit-resources/src/components/CreateCandidate.svelte +++ b/plugins/recruit-resources/src/components/CreateCandidate.svelte @@ -456,10 +456,7 @@ try { const uploadFile = await getResource(attachment.helper.UploadFile) - resume.uuid = await uploadFile(file, { - space: contact.space.Contacts, - attachedTo: candidateId - }) + resume.uuid = await uploadFile(file) resume.name = file.name resume.size = file.size resume.type = file.type diff --git a/server/core/src/utils.ts b/server/core/src/utils.ts deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/server/front/src/index.ts b/server/front/src/index.ts index 48412d9a06..ef7b7ad37b 100644 --- a/server/front/src/index.ts +++ b/server/front/src/index.ts @@ -243,26 +243,6 @@ export function start ( const token = authHeader.split(' ')[1] const payload = decodeToken(token) const uuid = await minioUpload(config.minio, payload.workspace, file) - // console.log('uploaded uuid', uuid) - - // const space = req.query.space as Ref | undefined - // const attachedTo = req.query.attachedTo as Ref | undefined - - // if (space !== undefined && attachedTo !== undefined) { - // const elastic = await createElasticAdapter(config.elasticUrl, payload.workspace) - - // const indexedDoc: IndexedDoc = { - // id: uuid as Ref, - // _class: attachment.class.Attachment, - // space, - // modifiedOn: Date.now(), - // modifiedBy: 'core:account:System' as Ref, - // attachedTo, - // data: file.data.toString('base64') - // } - - // await elastic.index(indexedDoc) - // } res.status(200).send(uuid) } catch (error) {