Zhi Chen 2c757c43c5
feat: user data sync plugin (#4986)
* feat: user data sync plugin

* fix: nickname bug

* feat: adjust sync

* fix: delete package-lock.json

* feat: adjust push

* feat: adjust user-data-sync plugin

* fix: delete submodule

* fix: sync bug

* fix: adjust log

* fix: adjust log

* chore: add to build-in

* chore: update version

* chore: add api doc

* chore: add plugin description

* chore: api doc

* chore: change keywords

* feat: add no source type prompt

* chore: text to json

* feat: optimize resource manager

* fix: test & typo

* test: add tests for resource manager & fix bug

* fix: user sync

* chore: remove department resource

* fix: test

* fix: build

* fix: push data

* fix: push api add default sourceName

* test: add api test

* fix: uniqueKey support any

* chore: single user belongs to multi department situation

* chore: department

* fix: fix sync bugs

* fix: sync departments

* chore: remove department logic

* chore: remove department

* fix: fix logger type error

* fix: fix sync bug

* fix: logger & role

* fix: fix sync bugs

* fix: fix sync bug

* fix: fix sync bugs

* test: update test cases

* chore: update

* chore: update

* fix: test

* fix: test

* fix: test

* fix: bugs

* fix: version

---------

Co-authored-by: chenzhi <chenzhi@zhuopaikeji.com>
Co-authored-by: xilesun <2013xile@gmail.com>
2024-08-27 05:25:30 +08:00

59 lines
2.1 KiB
TypeScript

/**
* This file is part of the NocoBase (R) project.
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
* Authors: NocoBase Team.
*
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
import { Registry } from '@nocobase/utils';
import { SyncSource, SyncSourceExtend } from './sync-source';
import { Context } from '@nocobase/actions';
import { SyncSourceModel } from './models/sync-source';
type SyncSourceConfig = {
syncSource: SyncSourceExtend<SyncSource>;
title?: string;
};
export class SyncSourceManager {
protected syncSourceTypes: Registry<SyncSourceConfig> = new Registry();
registerType(syncSourceType: string, syncSourceConfig: SyncSourceConfig) {
this.syncSourceTypes.register(syncSourceType, syncSourceConfig);
}
listTypes() {
return Array.from(this.syncSourceTypes.getEntities()).map(([syncSourceType, source]) => ({
name: syncSourceType,
title: source.title,
}));
}
async getByName(name: string, ctx: Context) {
const repo = ctx.db.getRepository('userDataSyncSources');
const sourceInstance: SyncSourceModel = await repo.findOne({ filter: { enabled: true, name: name } });
if (!sourceInstance) {
throw new Error(`SyncSource [${name}] is not found.`);
}
return this.create(sourceInstance, ctx);
}
async getById(id: number, ctx: Context) {
const repo = ctx.db.getRepository('userDataSyncSources');
const sourceInstance: SyncSourceModel = await repo.findOne({ filter: { enabled: true }, filterByTk: id });
if (!sourceInstance) {
throw new Error(`SyncSource [${id}] is not found.`);
}
return this.create(sourceInstance, ctx);
}
create(sourceInstance: SyncSourceModel, ctx: Context) {
const { syncSource } = this.syncSourceTypes.get(sourceInstance.sourceType) || {};
if (!syncSource) {
throw new Error(`SyncSourceType [${sourceInstance.sourceType}] is not found.`);
}
return new syncSource({ sourceInstance: sourceInstance, options: sourceInstance.options, ctx });
}
}