Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 23 additions & 58 deletions api/src/chat/repositories/block.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@

import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import {
Document,
Model,
Query,
Types,
UpdateQuery,
UpdateWithAggregationPipeline,
} from 'mongoose';
import { Model, Types } from 'mongoose';

import { BaseRepository, DeleteResult } from '@/utils/generics/base-repository';
import { TFilterQuery } from '@/utils/types/filter.types';
import { BaseRepository } from '@/utils/generics/base-repository';
import {
Args,
postDelete,
preCreate,
preDelete,
preUpdate,
preUpdateMany,
} from '@/utils/types/lifecycle-hook-manager.types';

import { BlockCreateDto, BlockDto, BlockUpdateDto } from '../dto/block.dto';
import {
Expand Down Expand Up @@ -63,10 +63,8 @@ export class BlockRepository extends BaseRepository<
*
* @param doc - The document that is being created.
*/
async preCreate(
_doc: Document<unknown, object, Block> & Block & { _id: Types.ObjectId },
): Promise<void> {
if (_doc) this.checkDeprecatedAttachmentUrl(_doc);
async preCreate(...[doc]: Args<preCreate<Block>>): Promise<void> {
if (doc) this.checkDeprecatedAttachmentUrl(doc);
}

/**
Expand All @@ -77,19 +75,9 @@ export class BlockRepository extends BaseRepository<
* @param updates - The update data.
*/
async preUpdate(
_query: Query<
Document<Block, any, any>,
Document<Block, any, any>,
unknown,
Block,
'findOneAndUpdate'
>,
criteria: TFilterQuery<Block>,
updates:
| UpdateWithAggregationPipeline
| UpdateQuery<Document<Block, any, any>>,
...[, criteria, updates]: Args<preUpdate<Block>>
): Promise<void> {
const update: BlockUpdateDto = updates?.['$set'];
const update = '$set' in updates ? updates.$set : {};

if (update?.category) {
const movedBlock = await this.findOne(criteria);
Expand All @@ -109,7 +97,10 @@ export class BlockRepository extends BaseRepository<
{ $set: { attachedBlock: null } },
);
}
this.checkDeprecatedAttachmentUrl(update);

if (update) {
this.checkDeprecatedAttachmentUrl(update);
}
}

/**
Expand All @@ -120,18 +111,10 @@ export class BlockRepository extends BaseRepository<
* @param updates - The update data.
*/
async preUpdateMany(
_query: Query<
Document<Block, any, any>,
Document<Block, any, any>,
unknown,
Block,
'updateMany',
Record<string, never>
>,
criteria: TFilterQuery<Block>,
updates: UpdateQuery<Document<Block, any, any>>,
...[, criteria, updates]: Args<preUpdateMany<Block>>
): Promise<void> {
const categoryId: string = updates.$set.category;
const categoryId = '$set' in updates && updates.$set?.category;

if (categoryId) {
const movedBlocks = await this.find(criteria);

Expand Down Expand Up @@ -226,16 +209,7 @@ export class BlockRepository extends BaseRepository<
* @param query - The delete query.
* @param result - The result of the delete operation.
*/
async postDelete(
_query: Query<
DeleteResult,
Document<Block, any, any>,
unknown,
Block,
'deleteOne' | 'deleteMany'
>,
result: DeleteResult,
) {
async postDelete(...[, result]: Args<postDelete<Block>>) {
if (result.deletedCount > 0) {
}
}
Comment on lines +212 to 215
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Remove empty conditional block or implement the missing logic.

The method contains an empty conditional block that serves no purpose. Either implement the intended logic or remove the condition entirely.

-  async postDelete(...[, result]: Args<postDelete<Block>>) {
-    if (result.deletedCount > 0) {
-    }
-  }
+  async postDelete(...[, result]: Args<postDelete<Block>>) {
+    // Implementation can be added when needed
+  }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
async postDelete(...[, result]: Args<postDelete<Block>>) {
if (result.deletedCount > 0) {
}
}
async postDelete(...[, result]: Args<postDelete<Block>>) {
// Implementation can be added when needed
}
🤖 Prompt for AI Agents
In api/src/chat/repositories/block.repository.ts around lines 212 to 215, there
is an empty if statement checking if result.deletedCount is greater than 0 with
no logic inside. You should either implement the intended logic that should run
when deletions occur or remove the entire if condition and its block to avoid
having an empty conditional statement.

Expand All @@ -247,16 +221,7 @@ export class BlockRepository extends BaseRepository<
* @param query - The delete query.
* @param criteria - The filter criteria for finding blocks to delete.
*/
async preDelete(
_query: Query<
DeleteResult,
Document<Block, any, any>,
unknown,
Block,
'deleteOne' | 'deleteMany'
>,
criteria: TFilterQuery<Block>,
) {
async preDelete(...[, criteria]: Args<preDelete<Block>>) {
const docsToDelete = await this.model.find(criteria);
const idsToDelete = docsToDelete.map(({ id }) => id);
if (idsToDelete.length > 0) {
Expand Down
17 changes: 4 additions & 13 deletions api/src/chat/repositories/category.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

import { ForbiddenException, Injectable, Optional } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Document, Model, Query } from 'mongoose';
import { Model } from 'mongoose';

import { BaseRepository, DeleteResult } from '@/utils/generics/base-repository';
import { TFilterQuery } from '@/utils/types/filter.types';
import { BaseRepository } from '@/utils/generics/base-repository';
import { Args, preDelete } from '@/utils/types/lifecycle-hook-manager.types';

import { CategoryDto } from '../dto/category.dto';
import { Category } from '../schemas/category.schema';
Expand Down Expand Up @@ -41,16 +41,7 @@ export class CategoryRepository extends BaseRepository<
* @param query - The delete query.
* @param criteria - The filter criteria for finding blocks to delete.
*/
async preDelete(
query: Query<
DeleteResult,
Document<Category, any, any>,
unknown,
Category,
'deleteOne' | 'deleteMany'
>,
criteria: TFilterQuery<Category>,
) {
async preDelete(...[query, criteria]: Args<preDelete<Category>>) {
criteria = query.getQuery();
const ids = Array.isArray(criteria._id?.$in)
? criteria._id.$in
Expand Down
17 changes: 4 additions & 13 deletions api/src/chat/repositories/context-var.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import {
Optional,
} from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Document, Model, Query } from 'mongoose';
import { Model } from 'mongoose';

import { BaseRepository, DeleteResult } from '@/utils/generics/base-repository';
import { TFilterQuery } from '@/utils/types/filter.types';
import { BaseRepository } from '@/utils/generics/base-repository';
import { Args, preDelete } from '@/utils/types/lifecycle-hook-manager.types';

import { ContextVarDto } from '../dto/context-var.dto';
import { ContextVar } from '../schemas/context-var.schema';
Expand Down Expand Up @@ -47,16 +47,7 @@ export class ContextVarRepository extends BaseRepository<
* @param query - The delete query.
* @param criteria - The filter criteria for finding context vars to delete.
*/
async preDelete(
_query: Query<
DeleteResult,
Document<ContextVar, any, any>,
unknown,
ContextVar,
'deleteOne' | 'deleteMany'
>,
criteria: TFilterQuery<ContextVar>,
) {
async preDelete(...[, criteria]: Args<preDelete<ContextVar>>) {
const ids = Array.isArray(criteria._id) ? criteria._id : [criteria._id];

for (const id of ids) {
Expand Down
24 changes: 9 additions & 15 deletions api/src/chat/repositories/label.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@

import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Document, Model, Query } from 'mongoose';
import { Model } from 'mongoose';

import { BaseRepository, DeleteResult } from '@/utils/generics/base-repository';
import { TFilterQuery } from '@/utils/types/filter.types';
import { BaseRepository } from '@/utils/generics/base-repository';
import {
Args,
postCreate,
preDelete,
} from '@/utils/types/lifecycle-hook-manager.types';

import { LabelDto } from '../dto/label.dto';
import {
Label,
LABEL_POPULATE,
LabelDocument,
LabelFull,
LabelPopulate,
} from '../schemas/label.schema';
Expand All @@ -40,7 +43,7 @@ export class LabelRepository extends BaseRepository<
*
* @returns A promise that resolves when the update operation is complete.
*/
async postCreate(created: LabelDocument): Promise<void> {
async postCreate(...[created]: Args<postCreate<Label>>): Promise<void> {
this.eventEmitter.emit(
'hook:label:create',
created,
Expand Down Expand Up @@ -68,16 +71,7 @@ export class LabelRepository extends BaseRepository<
*
* @returns {Promise<void>} A promise that resolves once the event is emitted.
*/
async preDelete(
_query: Query<
DeleteResult,
Document<Label, any, any>,
unknown,
Label,
'deleteOne' | 'deleteMany'
>,
_criteria: TFilterQuery<Label>,
): Promise<void> {
async preDelete(...[, _criteria]: Args<preDelete<Label>>): Promise<void> {
const ids = Array.isArray(_criteria._id?.$in)
? _criteria._id.$in
: Array.isArray(_criteria._id)
Expand Down
29 changes: 8 additions & 21 deletions api/src/chat/repositories/subscriber.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,20 @@

import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import {
Document,
Model,
Query,
UpdateQuery,
UpdateWithAggregationPipeline,
} from 'mongoose';
import { Model } from 'mongoose';

import { BotStatsType } from '@/analytics/schemas/bot-stats.schema';
import { BaseRepository } from '@/utils/generics/base-repository';
import { TFilterQuery } from '@/utils/types/filter.types';
import {
Args,
postCreate,
preUpdate,
} from '@/utils/types/lifecycle-hook-manager.types';

import { SubscriberDto, SubscriberUpdateDto } from '../dto/subscriber.dto';
import {
Subscriber,
SUBSCRIBER_POPULATE,
SubscriberDocument,
SubscriberFull,
SubscriberPopulate,
} from '../schemas/subscriber.schema';
Expand All @@ -45,7 +42,7 @@ export class SubscriberRepository extends BaseRepository<
*
* @param created - The newly created subscriber document.
*/
async postCreate(created: SubscriberDocument): Promise<void> {
async postCreate(...[created]: Args<postCreate<Subscriber>>): Promise<void> {
this.eventEmitter.emit(
'hook:stats:entry',
BotStatsType.new_users,
Expand All @@ -63,17 +60,7 @@ export class SubscriberRepository extends BaseRepository<
* @param updates - The update data, which may include fields like `assignedTo`.
*/
async preUpdate(
_query: Query<
Document<Subscriber, any, any>,
Document<Subscriber, any, any>,
unknown,
Subscriber,
'findOneAndUpdate'
>,
criteria: TFilterQuery<Subscriber>,
updates:
| UpdateWithAggregationPipeline
| UpdateQuery<Document<Subscriber, any, any>>,
...[, criteria, updates]: Args<preUpdate<Subscriber>>
): Promise<void> {
const subscriberUpdates: SubscriberUpdateDto = updates?.['$set'];

Expand Down
23 changes: 7 additions & 16 deletions api/src/cms/repositories/content-type.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

import { ForbiddenException, Injectable, Optional } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Document, Model, Query } from 'mongoose';
import { Model } from 'mongoose';

import { BlockService } from '@/chat/services/block.service';
import { BaseRepository, DeleteResult } from '@/utils/generics/base-repository';
import { TFilterQuery } from '@/utils/types/filter.types';
import { BaseRepository } from '@/utils/generics/base-repository';
import { Args, preDelete } from '@/utils/types/lifecycle-hook-manager.types';

import { ContentTypeDto } from '../dto/contentType.dto';
import { ContentType } from '../schemas/content-type.schema';
Expand Down Expand Up @@ -42,25 +42,16 @@ export class ContentTypeRepository extends BaseRepository<
* @param query - The query object used for deletion.
* @param criteria - The filter query to identify the content type entity to delete.
*/
async preDelete(
_query: Query<
DeleteResult,
Document<ContentType, any, any>,
unknown,
ContentType,
'deleteOne' | 'deleteMany'
>,
criteria: TFilterQuery<ContentType>,
) {
const entityId: string = criteria._id as string;
async preDelete(...[, criteria]: Args<preDelete<ContentType>>) {
const entityId = criteria._id;
const associatedBlocks = await this.blockService?.findOne({
'options.content.entity': entityId,
});
if (associatedBlocks) {
throw new ForbiddenException(`Content type have blocks associated to it`);
}
if (criteria._id) {
await this.contentModel.deleteMany({ entity: criteria._id });
if (entityId) {
await this.contentModel.deleteMany({ entity: entityId });
} else {
throw new Error(
'Attempted to delete content type using unknown criteria',
Expand Down
Loading