first commit
This commit is contained in:
@@ -0,0 +1,303 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import {
|
||||
FindActivityRepository,
|
||||
FindCommentsRepository,
|
||||
FindIssuesRepository,
|
||||
FindProjectsRepository,
|
||||
FindReleasesRepository,
|
||||
IssueFilter,
|
||||
Page,
|
||||
} from '@task-stream/api/tracker/application';
|
||||
import {
|
||||
ActivityReadModel,
|
||||
CommentReadModel,
|
||||
CountBy,
|
||||
FINISHED_STATUSES,
|
||||
IssueReadModel,
|
||||
IssueStatus,
|
||||
ProjectReadModel,
|
||||
ProjectRole,
|
||||
ProjectStatsReadModel,
|
||||
ReleaseProgressReadModel,
|
||||
ReleaseReadModel,
|
||||
UserRefReadModel,
|
||||
} from '@task-stream/api/tracker/domain';
|
||||
import { FilterQuery, Model } from 'mongoose';
|
||||
import {
|
||||
ActivityDoc,
|
||||
CommentDoc,
|
||||
IssueDoc,
|
||||
ProjectDoc,
|
||||
ReadModels,
|
||||
ReleaseDoc,
|
||||
fromDoc,
|
||||
issueFromDoc,
|
||||
} from './read-model.schemas';
|
||||
|
||||
/* Query-side adapters (MongoDB). */
|
||||
|
||||
const escapeRegex = (text: string) =>
|
||||
text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
|
||||
function toProject(doc: ProjectDoc): ProjectReadModel {
|
||||
const { memberIds: _ignored, ...rest } = doc;
|
||||
return fromDoc(rest);
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class MongoFindProjectsRepository extends FindProjectsRepository {
|
||||
constructor(
|
||||
@InjectModel(ReadModels.Project)
|
||||
private readonly projects: Model<ProjectDoc>,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<ProjectReadModel | null> {
|
||||
const doc = await this.projects.findById(id).lean<ProjectDoc>();
|
||||
return doc ? toProject(doc) : null;
|
||||
}
|
||||
|
||||
async findByKey(key: string): Promise<ProjectReadModel | null> {
|
||||
const doc = await this.projects.findOne({ key }).lean<ProjectDoc>();
|
||||
return doc ? toProject(doc) : null;
|
||||
}
|
||||
|
||||
async findByMember(userId: string): Promise<ProjectReadModel[]> {
|
||||
const docs = await this.projects
|
||||
.find({ memberIds: userId })
|
||||
.sort({ name: 1 })
|
||||
.lean<ProjectDoc[]>();
|
||||
return docs.map(toProject);
|
||||
}
|
||||
|
||||
async findRole(
|
||||
projectId: string,
|
||||
userId: string,
|
||||
): Promise<ProjectRole | null> {
|
||||
const doc = await this.projects
|
||||
.findOne({ _id: projectId, memberIds: userId }, { members: 1 })
|
||||
.lean<Pick<ProjectDoc, 'members'>>();
|
||||
return doc?.members.find((m) => m.user.id === userId)?.role ?? null;
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class MongoFindIssuesRepository extends FindIssuesRepository {
|
||||
constructor(
|
||||
@InjectModel(ReadModels.Issue) private readonly issues: Model<IssueDoc>,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<IssueReadModel | null> {
|
||||
const doc = await this.issues.findById(id).lean<IssueDoc>();
|
||||
return doc ? issueFromDoc(doc) : null;
|
||||
}
|
||||
|
||||
async findByKey(key: string): Promise<IssueReadModel | null> {
|
||||
const doc = await this.issues.findOne({ key }).lean<IssueDoc>();
|
||||
return doc ? issueFromDoc(doc) : null;
|
||||
}
|
||||
|
||||
async findByIds(ids: string[]): Promise<IssueReadModel[]> {
|
||||
if (ids.length === 0) return [];
|
||||
const docs = await this.issues
|
||||
.find({ _id: { $in: ids } })
|
||||
.lean<IssueDoc[]>();
|
||||
return docs.map(issueFromDoc);
|
||||
}
|
||||
|
||||
async search(filter: IssueFilter): Promise<Page<IssueReadModel>> {
|
||||
const query = toMongoFilter(filter);
|
||||
const [docs, total] = await Promise.all([
|
||||
this.issues
|
||||
.find(query)
|
||||
.sort({ updatedAt: -1 })
|
||||
.skip(filter.offset)
|
||||
.limit(filter.limit)
|
||||
.lean<IssueDoc[]>(),
|
||||
this.issues.countDocuments(query),
|
||||
]);
|
||||
return { items: docs.map(issueFromDoc), total };
|
||||
}
|
||||
|
||||
async stats(projectId: string): Promise<ProjectStatsReadModel> {
|
||||
const countBy = (field: string) => [
|
||||
{ $group: { _id: `$${field}`, count: { $sum: 1 } } },
|
||||
{ $sort: { count: -1 as const } },
|
||||
];
|
||||
const [facets] = await this.issues.aggregate<{
|
||||
total: { count: number }[];
|
||||
open: { count: number }[];
|
||||
byStatus: { _id: IssueStatus; count: number }[];
|
||||
byType: { _id: string; count: number }[];
|
||||
byPriority: { _id: string; count: number }[];
|
||||
byArea: { _id: string | null; count: number }[];
|
||||
byAssignee: { _id: UserRefReadModel | null; count: number }[];
|
||||
}>([
|
||||
{ $match: { projectId } },
|
||||
{
|
||||
$facet: {
|
||||
total: [{ $count: 'count' }],
|
||||
open: [
|
||||
{ $match: { status: { $nin: FINISHED_STATUSES } } },
|
||||
{ $count: 'count' },
|
||||
],
|
||||
byStatus: countBy('status'),
|
||||
byType: countBy('type'),
|
||||
byPriority: countBy('priority'),
|
||||
byArea: countBy('area'),
|
||||
byAssignee: [
|
||||
{ $match: { status: { $nin: FINISHED_STATUSES } } },
|
||||
...countBy('assignee'),
|
||||
],
|
||||
},
|
||||
},
|
||||
]);
|
||||
const keyed = <T>(rows: { _id: unknown; count: number }[]): CountBy<T>[] =>
|
||||
rows.map((r) => ({ key: (r._id ?? 'NONE') as T, count: r.count }));
|
||||
|
||||
return {
|
||||
total: facets.total[0]?.count ?? 0,
|
||||
open: facets.open[0]?.count ?? 0,
|
||||
byStatus: keyed(facets.byStatus),
|
||||
byType: keyed(facets.byType),
|
||||
byPriority: keyed(facets.byPriority),
|
||||
byArea: keyed(facets.byArea),
|
||||
byAssignee: facets.byAssignee.map((r) => ({
|
||||
user: r._id?.id ? r._id : null,
|
||||
count: r.count,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
async releaseProgress(releaseId: string): Promise<ReleaseProgressReadModel> {
|
||||
const rows = await this.issues.aggregate<{
|
||||
_id: IssueStatus;
|
||||
count: number;
|
||||
}>([
|
||||
{ $match: { 'release.id': releaseId } },
|
||||
{ $group: { _id: '$status', count: { $sum: 1 } } },
|
||||
]);
|
||||
const total = rows.reduce((sum, r) => sum + r.count, 0);
|
||||
const finished = rows
|
||||
.filter((r) => FINISHED_STATUSES.includes(r._id))
|
||||
.reduce((sum, r) => sum + r.count, 0);
|
||||
return {
|
||||
total,
|
||||
finished,
|
||||
percent: total === 0 ? 0 : Math.round((finished / total) * 100),
|
||||
byStatus: rows.map((r) => ({ key: r._id, count: r.count })),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function toMongoFilter(filter: IssueFilter): FilterQuery<IssueDoc> {
|
||||
const query: FilterQuery<IssueDoc> = { projectId: filter.projectId };
|
||||
if (filter.statuses?.length) query.status = { $in: filter.statuses };
|
||||
if (filter.types?.length) query['type'] = { $in: filter.types };
|
||||
if (filter.areas?.length) query['area'] = { $in: filter.areas };
|
||||
if (filter.priorities?.length) query['priority'] = { $in: filter.priorities };
|
||||
if (filter.labels?.length)
|
||||
query.labels = { $all: filter.labels.map((l) => l.toLowerCase()) };
|
||||
if (filter.releaseId) query['release.id'] = filter.releaseId;
|
||||
if (filter.unassigned) query['assignee'] = null;
|
||||
else if (filter.assigneeId) query['assignee.id'] = filter.assigneeId;
|
||||
if (filter.search?.trim()) {
|
||||
const pattern = new RegExp(escapeRegex(filter.search.trim()), 'i');
|
||||
query.$or = [
|
||||
{ title: pattern },
|
||||
{ key: pattern },
|
||||
{ description: pattern },
|
||||
];
|
||||
}
|
||||
return query;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class MongoFindCommentsRepository extends FindCommentsRepository {
|
||||
constructor(
|
||||
@InjectModel(ReadModels.Comment)
|
||||
private readonly comments: Model<CommentDoc>,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<CommentReadModel | null> {
|
||||
const doc = await this.comments.findById(id).lean<CommentDoc>();
|
||||
return doc ? fromDoc(doc) : null;
|
||||
}
|
||||
|
||||
async findByIssue(
|
||||
issueId: string,
|
||||
limit: number,
|
||||
offset: number,
|
||||
): Promise<Page<CommentReadModel>> {
|
||||
const [docs, total] = await Promise.all([
|
||||
this.comments
|
||||
.find({ issueId })
|
||||
.sort({ createdAt: 1 })
|
||||
.skip(offset)
|
||||
.limit(limit)
|
||||
.lean<CommentDoc[]>(),
|
||||
this.comments.countDocuments({ issueId }),
|
||||
]);
|
||||
return { items: docs.map(fromDoc), total };
|
||||
}
|
||||
|
||||
countByIssue(issueId: string): Promise<number> {
|
||||
return this.comments.countDocuments({ issueId });
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class MongoFindReleasesRepository extends FindReleasesRepository {
|
||||
constructor(
|
||||
@InjectModel(ReadModels.Release)
|
||||
private readonly releases: Model<ReleaseDoc>,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<ReleaseReadModel | null> {
|
||||
const doc = await this.releases.findById(id).lean<ReleaseDoc>();
|
||||
return doc ? fromDoc(doc) : null;
|
||||
}
|
||||
|
||||
async findByProject(projectId: string): Promise<ReleaseReadModel[]> {
|
||||
const docs = await this.releases
|
||||
.find({ projectId })
|
||||
.sort({ createdAt: -1 })
|
||||
.lean<ReleaseDoc[]>();
|
||||
return docs.map(fromDoc);
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class MongoFindActivityRepository extends FindActivityRepository {
|
||||
constructor(
|
||||
@InjectModel(ReadModels.Activity)
|
||||
private readonly activity: Model<ActivityDoc>,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
async find(params: {
|
||||
projectId: string;
|
||||
issueId?: string;
|
||||
limit: number;
|
||||
before?: string;
|
||||
}): Promise<ActivityReadModel[]> {
|
||||
const query: FilterQuery<ActivityDoc> = { projectId: params.projectId };
|
||||
if (params.issueId) query.issueId = params.issueId;
|
||||
if (params.before) query.occurredAt = { $lt: params.before };
|
||||
const docs = await this.activity
|
||||
.find(query)
|
||||
.sort({ occurredAt: -1 })
|
||||
.limit(params.limit)
|
||||
.lean<ActivityDoc[]>();
|
||||
return docs.map(fromDoc);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user