mirror of
https://github.com/hcengineering/platform.git
synced 2026-10-01 05:55:09 +02:00
@@ -15,14 +15,22 @@
|
||||
//
|
||||
-->
|
||||
<script lang="ts">
|
||||
import presentation, { Card, createQuery } from '@hcengineering/presentation'
|
||||
import presentation, { Card, createQuery, getClient } from '@hcengineering/presentation'
|
||||
import { Integration } from '@hcengineering/setting'
|
||||
import { createEventDispatcher, onMount } from 'svelte'
|
||||
import bitrix from '../plugin'
|
||||
|
||||
import { BitrixClient, BitrixEntityMapping, BitrixProfile, StatusValue } from '@hcengineering/bitrix'
|
||||
import {
|
||||
BitrixClient,
|
||||
BitrixEntityMapping,
|
||||
BitrixFieldMapping,
|
||||
BitrixProfile,
|
||||
StatusValue
|
||||
} from '@hcengineering/bitrix'
|
||||
import { Button, eventToHTMLElement, IconAdd, showPopup } from '@hcengineering/ui'
|
||||
|
||||
import { Data, Doc, Ref } from '@hcengineering/core'
|
||||
import { getEmbeddedLabel } from '@hcengineering/platform'
|
||||
import { bitrixQueue } from '../queue'
|
||||
import CreateMapping from './CreateMapping.svelte'
|
||||
import EntityMapping from './EntityMapping.svelte'
|
||||
@@ -47,6 +55,9 @@
|
||||
})
|
||||
})
|
||||
|
||||
let inputFile: HTMLInputElement
|
||||
const client = getClient()
|
||||
|
||||
const mQuery = createQuery()
|
||||
|
||||
let mappings: BitrixEntityMapping[] = []
|
||||
@@ -57,6 +68,68 @@
|
||||
function addMapping (evt: MouseEvent): void {
|
||||
showPopup(CreateMapping, { integration, mappings, bitrixClient }, eventToHTMLElement(evt))
|
||||
}
|
||||
const signature = '@#253heyf@'
|
||||
const downloadConfig = async () => {
|
||||
const filename = 'bitrix-config_' + new Date().toLocaleDateString() + '.json'
|
||||
const link = document.createElement('a')
|
||||
const fields = await client.findAll(bitrix.class.FieldMapping, {})
|
||||
link.style.display = 'none'
|
||||
link.setAttribute('target', '_blank')
|
||||
link.setAttribute(
|
||||
'href',
|
||||
'data:text/json;charset=utf-8,%EF%BB%BF' +
|
||||
encodeURIComponent(JSON.stringify({ mappings, fields, signature }, undefined, 2))
|
||||
)
|
||||
link.setAttribute('download', filename)
|
||||
document.body.appendChild(link)
|
||||
link.click()
|
||||
document.body.removeChild(link)
|
||||
}
|
||||
const replaceConfig = async () => {
|
||||
inputFile.click()
|
||||
}
|
||||
|
||||
const fileSelected = async () => {
|
||||
const file = inputFile.files?.[0]
|
||||
const text = await file?.text()
|
||||
if (text === undefined) {
|
||||
return
|
||||
}
|
||||
const jsonParsed = JSON.parse(text)
|
||||
if (jsonParsed.signature !== signature) {
|
||||
return
|
||||
}
|
||||
|
||||
const op = client.apply('bitrix')
|
||||
// Remove all stuff
|
||||
for (const d of await (
|
||||
await client.findAll<Doc>(bitrix.class.EntityMapping, {})
|
||||
).concat(...(await client.findAll<Doc>(bitrix.class.FieldMapping, {})))) {
|
||||
await op.remove(d)
|
||||
}
|
||||
|
||||
// Import new items.
|
||||
const mappings = jsonParsed.mappings as BitrixEntityMapping[]
|
||||
for (const m of mappings) {
|
||||
const { _class, space, _id, ...dta } = m
|
||||
await op.tx(op.txFactory.createTxCreateDoc(_class, space, dta, _id))
|
||||
}
|
||||
const fields = jsonParsed.fields as BitrixFieldMapping[]
|
||||
for (const m of fields) {
|
||||
const { _class, space, _id, attachedTo, attachedToClass, collection, modifiedBy, modifiedOn, ...dta } = m
|
||||
const cr = op.txFactory.createTxCreateDoc<BitrixFieldMapping>(_class, space, dta as Data<BitrixFieldMapping>, _id)
|
||||
const col = op.txFactory.createTxCollectionCUD<BitrixEntityMapping, BitrixFieldMapping>(
|
||||
attachedToClass,
|
||||
attachedTo as Ref<BitrixEntityMapping>,
|
||||
space,
|
||||
collection,
|
||||
cr
|
||||
)
|
||||
await op.tx(col)
|
||||
}
|
||||
|
||||
await op.commit()
|
||||
}
|
||||
</script>
|
||||
|
||||
<Card
|
||||
@@ -78,15 +151,20 @@
|
||||
{/if}
|
||||
</svelte:fragment>
|
||||
{#if profile}
|
||||
<div class="flex flex-reverse flex-grab">
|
||||
<Button icon={IconAdd} label={presentation.string.Add} on:click={addMapping} />
|
||||
</div>
|
||||
<div class="flex-row">
|
||||
{#each mappings as mapping}
|
||||
<EntityMapping {mapping} {bitrixClient} {statusList} />
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
<!-- <EditBox label={bitrix.string.BitrixTokenUrl} bind:value={url} /> -->
|
||||
<svelte:fragment slot="pool" />
|
||||
<svelte:fragment slot="pool">
|
||||
<div class="flex-row-center flex-grow flex-between">
|
||||
<Button icon={IconAdd} label={presentation.string.Add} on:click={addMapping} />
|
||||
<div class="flex-row-center">
|
||||
<Button label={getEmbeddedLabel('Download...')} on:click={downloadConfig} />
|
||||
<Button label={getEmbeddedLabel('Replace...')} on:click={replaceConfig} />
|
||||
<input bind:this={inputFile} type="file" name="file" id="file" style="display: none" on:change={fileSelected} />
|
||||
</div>
|
||||
</div>
|
||||
</svelte:fragment>
|
||||
</Card>
|
||||
|
||||
Reference in New Issue
Block a user