mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-07-02 03:02:19 +08:00
fix: move action (#3985)
This commit is contained in:
parent
771fdf48fc
commit
dc8dbdd1cb
@ -1,38 +1,24 @@
|
|||||||
import { Model, Op } from 'sequelize';
|
import { Model, Op } from 'sequelize';
|
||||||
|
|
||||||
|
import { BelongsToManyRepository, Collection, HasManyRepository, SortField, TargetKey } from '@nocobase/database';
|
||||||
import { Context } from '..';
|
import { Context } from '..';
|
||||||
import {
|
|
||||||
BelongsToManyRepository,
|
|
||||||
Collection,
|
|
||||||
HasManyRepository,
|
|
||||||
Repository,
|
|
||||||
SortField,
|
|
||||||
TargetKey,
|
|
||||||
} from '@nocobase/database';
|
|
||||||
import { getRepositoryFromParams } from '../utils';
|
import { getRepositoryFromParams } from '../utils';
|
||||||
|
|
||||||
export async function move(ctx: Context, next) {
|
export async function move(ctx: Context, next) {
|
||||||
const repository = ctx.databaseRepository || getRepositoryFromParams(ctx);
|
const repository = ctx.databaseRepository || getRepositoryFromParams(ctx);
|
||||||
|
const { sourceId, targetId, targetScope, sticky, method } = ctx.action.params;
|
||||||
|
|
||||||
const { sourceId, targetId, sortField, targetScope, sticky, method } = ctx.action.params;
|
let sortField = ctx.action.params.sortField;
|
||||||
|
|
||||||
if (repository instanceof BelongsToManyRepository) {
|
if (repository instanceof BelongsToManyRepository) {
|
||||||
throw new Error("Sorting association as 'belongs-to-many' type is not supported.");
|
throw new Error("Sorting association as 'belongs-to-many' type is not supported.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (repository instanceof HasManyRepository) {
|
if (repository instanceof HasManyRepository && !sortField) {
|
||||||
const hasManyField = repository.sourceCollection.getField(repository.associationName);
|
sortField = `${repository.association.foreignKey}Sort`;
|
||||||
if (!hasManyField.options.sortable) {
|
|
||||||
throw new Error(
|
|
||||||
`association ${hasManyField.options.name} in ${repository.sourceCollection.name} is not sortable`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const sortAbleCollection = new SortAbleCollection(
|
const sortAbleCollection = new SortAbleCollection(repository.collection, sortField);
|
||||||
repository instanceof Repository ? repository.collection : repository.targetCollection,
|
|
||||||
repository instanceof Repository ? sortField : `${repository.association.foreignKey}Sort`,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (sourceId && targetId) {
|
if (sourceId && targetId) {
|
||||||
await sortAbleCollection.move(sourceId, targetId, {
|
await sortAbleCollection.move(sourceId, targetId, {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user