-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
217 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright © 2024 Hexastack. All rights reserved. | ||
* | ||
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms: | ||
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. | ||
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). | ||
*/ | ||
|
||
import { Injectable } from '@nestjs/common'; | ||
import { EventEmitter2 } from '@nestjs/event-emitter'; | ||
import { InjectModel } from '@nestjs/mongoose'; | ||
import { Model } from 'mongoose'; | ||
|
||
import { BaseRepository } from '@/utils/generics/base-repository'; | ||
|
||
import { | ||
Thread, | ||
THREAD_POPULATE, | ||
ThreadFull, | ||
ThreadPopulate, | ||
} from '../schemas/thread.schema'; | ||
|
||
@Injectable() | ||
export class ThreadRepository extends BaseRepository< | ||
Thread, | ||
ThreadPopulate, | ||
ThreadFull | ||
> { | ||
constructor( | ||
readonly eventEmitter: EventEmitter2, | ||
@InjectModel(Thread.name) readonly model: Model<Thread>, | ||
) { | ||
super(eventEmitter, model, Thread, THREAD_POPULATE, ThreadFull); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright © 2024 Hexastack. All rights reserved. | ||
* | ||
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms: | ||
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. | ||
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). | ||
*/ | ||
|
||
import { ModelDefinition, Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; | ||
import { Exclude, Transform, Type } from 'class-transformer'; | ||
import { Schema as MongooseSchema } from 'mongoose'; | ||
|
||
import { BaseSchema } from '@/utils/generics/base-schema'; | ||
import { LifecycleHookManager } from '@/utils/generics/lifecycle-hook-manager'; | ||
import { | ||
TFilterPopulateFields, | ||
THydratedDocument, | ||
} from '@/utils/types/filter.types'; | ||
|
||
import { Message } from './message.schema'; | ||
import { Subscriber } from './subscriber.schema'; | ||
|
||
@Schema({ timestamps: true }) | ||
export class ThreadStub extends BaseSchema { | ||
@Prop({ | ||
type: String, | ||
unique: true, | ||
required: true, | ||
}) | ||
title: string; | ||
|
||
@Prop({ | ||
type: MongooseSchema.Types.ObjectId, | ||
required: false, | ||
ref: 'Subscriber', | ||
}) | ||
subscriber?: unknown; | ||
} | ||
|
||
@Schema({ timestamps: true }) | ||
export class Thread extends ThreadStub { | ||
@Transform(({ obj }) => obj.subscriber?.toString()) | ||
subscriber: string; | ||
|
||
@Exclude() | ||
messages?: never; | ||
} | ||
|
||
@Schema({ timestamps: true }) | ||
export class ThreadFull extends ThreadStub { | ||
@Type(() => Subscriber) | ||
subscriber: Subscriber; | ||
|
||
@Type(() => Message) | ||
messages?: Message[]; | ||
} | ||
|
||
export type ThreadDocument = THydratedDocument<Thread>; | ||
|
||
export const ThreadModel: ModelDefinition = LifecycleHookManager.attach({ | ||
name: Thread.name, | ||
schema: SchemaFactory.createForClass(ThreadStub), | ||
}); | ||
|
||
ThreadModel.schema.virtual('messages', { | ||
ref: 'Message', | ||
localField: '_id', | ||
foreignField: 'thread', | ||
justOne: false, | ||
}); | ||
|
||
export default ThreadModel.schema; | ||
|
||
export type ThreadPopulate = keyof TFilterPopulateFields<Thread, ThreadStub>; | ||
|
||
export const THREAD_POPULATE: ThreadPopulate[] = ['messages', 'subscriber']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright © 2024 Hexastack. All rights reserved. | ||
* | ||
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms: | ||
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. | ||
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). | ||
*/ | ||
|
||
import { Injectable } from '@nestjs/common'; | ||
|
||
import { BaseService } from '@/utils/generics/base-service'; | ||
|
||
import { ThreadRepository } from '../repositories/thread.repository'; | ||
import { SubscriberStub } from '../schemas/subscriber.schema'; | ||
import { Thread, ThreadFull, ThreadPopulate } from '../schemas/thread.schema'; | ||
|
||
@Injectable() | ||
export class ThreadService extends BaseService< | ||
Thread, | ||
ThreadPopulate, | ||
ThreadFull | ||
> { | ||
constructor(private readonly threadRepository: ThreadRepository) { | ||
super(threadRepository); | ||
} | ||
|
||
/** | ||
* Retrieves the latest thread for a given subscriber | ||
* | ||
* @param subscriber The subscriber whose thread is being retrieved. | ||
* | ||
* @returns Last thread | ||
*/ | ||
async findLast<S extends SubscriberStub>(subscriber: S) { | ||
const [thread] = await this.findPage( | ||
{ | ||
subscriber: subscriber.id, | ||
}, | ||
{ skip: 0, limit: 1, sort: ['createdAt', 'desc'] }, | ||
); | ||
return thread; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters