mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
fix: address review
This commit is contained in:
parent
e0a7a783f9
commit
82e47f23dd
@ -90,27 +90,24 @@ export class BlockRepository extends BaseRepository<
|
|||||||
'findOneAndUpdate'
|
'findOneAndUpdate'
|
||||||
>,
|
>,
|
||||||
criteria: TFilterQuery<Block>,
|
criteria: TFilterQuery<Block>,
|
||||||
_updates:
|
updates:
|
||||||
| UpdateWithAggregationPipeline
|
| UpdateWithAggregationPipeline
|
||||||
| UpdateQuery<Document<Block, any, any>>,
|
| UpdateQuery<Document<Block, any, any>>,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const updates: BlockUpdateDto = _updates?.['$set'];
|
const update: BlockUpdateDto = updates?.['$set'];
|
||||||
if (updates?.category) {
|
if (update?.category && criteria._id) {
|
||||||
const movedBlockId = criteria._id;
|
const movedBlockId = criteria._id;
|
||||||
|
|
||||||
// Find and update blocks that reference the moved block
|
// Find and update blocks that reference the moved block
|
||||||
await this.model.updateMany(
|
|
||||||
{ nextBlocks: movedBlockId },
|
|
||||||
{ $pull: { nextBlocks: movedBlockId } },
|
|
||||||
);
|
|
||||||
|
|
||||||
await this.model.updateMany(
|
await this.model.updateMany(
|
||||||
{ attachedBlock: movedBlockId },
|
{ attachedBlock: movedBlockId },
|
||||||
{ $set: { attachedBlock: null } },
|
{ $set: { attachedBlock: null }, $pull: { nextBlocks: movedBlockId } },
|
||||||
);
|
);
|
||||||
|
} else if (update?.category && !criteria._id) {
|
||||||
|
throw new Error('Criteria must include a valid id to update category.');
|
||||||
}
|
}
|
||||||
|
|
||||||
this.checkDeprecatedAttachmentUrl(updates);
|
this.checkDeprecatedAttachmentUrl(update);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -129,68 +126,70 @@ export class BlockRepository extends BaseRepository<
|
|||||||
'updateMany',
|
'updateMany',
|
||||||
Record<string, never>
|
Record<string, never>
|
||||||
>,
|
>,
|
||||||
_criteria: TFilterQuery<Block>,
|
criteria: TFilterQuery<Block>,
|
||||||
_updates: UpdateQuery<Document<Block, any, any>>,
|
updates: UpdateQuery<Document<Block, any, any>>,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const ids: string[] = _criteria._id?.$in || [];
|
if (criteria._id?.$in && updates?.$set?.category) {
|
||||||
const objIds = ids.map((b) => {
|
const ids: string[] = criteria._id?.$in || [];
|
||||||
return new mongoose.Types.ObjectId(b);
|
const objIds = ids.map((b) => {
|
||||||
});
|
return new mongoose.Types.ObjectId(b);
|
||||||
const category: string = _updates.$set.category;
|
});
|
||||||
const objCategory = new mongoose.Types.ObjectId(category);
|
const category: string = updates.$set.category;
|
||||||
const otherBlocks = await this.model.find({
|
const objCategory = new mongoose.Types.ObjectId(category);
|
||||||
_id: { $nin: objIds },
|
const otherBlocks = await this.model.find({
|
||||||
category: { $ne: objCategory },
|
_id: { $nin: objIds },
|
||||||
$or: [
|
category: { $ne: objCategory },
|
||||||
{ attachedBlock: { $in: objIds } },
|
$or: [
|
||||||
{ nextBlocks: { $in: objIds } },
|
{ attachedBlock: { $in: objIds } },
|
||||||
],
|
{ nextBlocks: { $in: objIds } },
|
||||||
});
|
],
|
||||||
|
|
||||||
for (const id of ids) {
|
|
||||||
const oldState = await this.model.findOne({
|
|
||||||
_id: new mongoose.Types.ObjectId(id),
|
|
||||||
});
|
});
|
||||||
if (oldState.category.toString() !== category) {
|
|
||||||
const updatedNextBlocks = oldState.nextBlocks.filter((nextBlock) =>
|
|
||||||
ids.includes(nextBlock.toString()),
|
|
||||||
);
|
|
||||||
|
|
||||||
const updatedAttachedBlock = ids.includes(
|
for (const id of ids) {
|
||||||
oldState.attachedBlock?.toString() || '',
|
const oldState = await this.model.findOne({
|
||||||
)
|
_id: new mongoose.Types.ObjectId(id),
|
||||||
? oldState.attachedBlock
|
});
|
||||||
: null;
|
if (oldState.category.toString() !== category) {
|
||||||
|
const updatedNextBlocks = oldState.nextBlocks.filter((nextBlock) =>
|
||||||
|
ids.includes(nextBlock.toString()),
|
||||||
|
);
|
||||||
|
|
||||||
await this.model.updateOne(
|
const updatedAttachedBlock = ids.includes(
|
||||||
{ _id: new mongoose.Types.ObjectId(id) },
|
oldState.attachedBlock?.toString() || '',
|
||||||
{
|
)
|
||||||
nextBlocks: updatedNextBlocks,
|
? oldState.attachedBlock
|
||||||
attachedBlock: updatedAttachedBlock,
|
: null;
|
||||||
},
|
|
||||||
);
|
await this.model.updateOne(
|
||||||
|
{ _id: new mongoose.Types.ObjectId(id) },
|
||||||
|
{
|
||||||
|
nextBlocks: updatedNextBlocks,
|
||||||
|
attachedBlock: updatedAttachedBlock,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for (const block of otherBlocks) {
|
for (const block of otherBlocks) {
|
||||||
if (ids.includes(block.attachedBlock?.toString())) {
|
if (ids.includes(block.attachedBlock?.toString())) {
|
||||||
await this.model.updateOne(
|
await this.model.updateOne(
|
||||||
{ _id: block.id },
|
{ _id: block.id },
|
||||||
{
|
{
|
||||||
attachedBlock: null,
|
attachedBlock: null,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (block.nextBlocks.some((item) => ids.includes(item.toString()))) {
|
if (block.nextBlocks.some((item) => ids.includes(item.toString()))) {
|
||||||
const updatedNextBlocks = block.nextBlocks.filter(
|
const updatedNextBlocks = block.nextBlocks.filter(
|
||||||
(nextBlock) => !ids.includes(nextBlock.toString()),
|
(nextBlock) => !ids.includes(nextBlock.toString()),
|
||||||
);
|
);
|
||||||
await this.model.updateOne(
|
await this.model.updateOne(
|
||||||
{ _id: block.id },
|
{ _id: block.id },
|
||||||
{
|
{
|
||||||
nextBlocks: updatedNextBlocks,
|
nextBlocks: updatedNextBlocks,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -172,6 +172,15 @@ export abstract class BaseRepository<
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
hooks?.updateMany.post.execute(async function (updated: any) {
|
||||||
|
const query = this as Query<D, D, unknown, T, 'updateMany'>;
|
||||||
|
await repository.postUpdateMany(query, updated);
|
||||||
|
repository.emitter.emit(
|
||||||
|
repository.getEventName(EHook.postUpdateMany),
|
||||||
|
updated,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
hooks?.findOneAndUpdate.post.execute(async function (
|
hooks?.findOneAndUpdate.post.execute(async function (
|
||||||
updated: HydratedDocument<T>,
|
updated: HydratedDocument<T>,
|
||||||
) {
|
) {
|
||||||
@ -398,6 +407,13 @@ export abstract class BaseRepository<
|
|||||||
// Nothing ...
|
// Nothing ...
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async postUpdateMany(
|
||||||
|
_query: Query<D, D, unknown, T, 'updateMany'>,
|
||||||
|
_updated: any,
|
||||||
|
) {
|
||||||
|
// Nothing ...
|
||||||
|
}
|
||||||
|
|
||||||
async postUpdate(
|
async postUpdate(
|
||||||
_query: Query<D, D, unknown, T, 'findOneAndUpdate'>,
|
_query: Query<D, D, unknown, T, 'findOneAndUpdate'>,
|
||||||
_updated: T,
|
_updated: T,
|
||||||
|
@ -49,7 +49,7 @@ export const useUpdateMany = <
|
|||||||
ids: string[];
|
ids: string[];
|
||||||
payload: Partial<TAttr>;
|
payload: Partial<TAttr>;
|
||||||
}) => {
|
}) => {
|
||||||
const result = await api.UpdateMany(ids, payload);
|
const result = await api.updateMany(ids, payload);
|
||||||
|
|
||||||
queryClient.removeQueries({
|
queryClient.removeQueries({
|
||||||
predicate: ({ queryKey }) => {
|
predicate: ({ queryKey }) => {
|
||||||
|
@ -345,7 +345,7 @@ export class EntityApiClient<TAttr, TBasic, TFull> extends ApiClient {
|
|||||||
/**
|
/**
|
||||||
* Bulk Update entries.
|
* Bulk Update entries.
|
||||||
*/
|
*/
|
||||||
async UpdateMany(ids: string[], payload: Partial<TAttr>) {
|
async updateMany(ids: string[], payload: Partial<TAttr>) {
|
||||||
const { _csrf } = await this.getCsrf();
|
const { _csrf } = await this.getCsrf();
|
||||||
const { data } = await this.request.patch<string>(
|
const { data } = await this.request.patch<string>(
|
||||||
`${ROUTES[this.type]}/bulk`,
|
`${ROUTES[this.type]}/bulk`,
|
||||||
|
Loading…
Reference in New Issue
Block a user