ballrush-core 0.4.0 → 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/domain/group.d.ts +0 -4
- package/dist/domain/group.js +2 -4
- package/dist/domain/index.d.ts +1 -0
- package/dist/domain/index.js +1 -0
- package/dist/domain/stat-group.d.ts +26 -0
- package/dist/domain/stat-group.js +35 -0
- package/dist/mongo/index.d.ts +1 -0
- package/dist/mongo/index.js +1 -0
- package/dist/mongo/schemas/group.schema.d.ts +0 -6
- package/dist/mongo/schemas/group.schema.js +0 -6
- package/dist/mongo/schemas/stat-group.schema.d.ts +15 -0
- package/dist/mongo/schemas/stat-group.schema.js +19 -0
- package/dist/ports/groups.repository.d.ts +0 -39
- package/dist/ports/index.d.ts +1 -0
- package/dist/ports/index.js +1 -0
- package/dist/ports/stat-group.repository.d.ts +41 -0
- package/dist/ports/stat-group.repository.js +2 -0
- package/dist/repositories/index.d.ts +1 -0
- package/dist/repositories/index.js +1 -0
- package/dist/repositories/mongo/group.mapper.js +0 -2
- package/dist/repositories/mongo/group.repository.d.ts +1 -3
- package/dist/repositories/mongo/group.repository.js +0 -93
- package/dist/repositories/mongo/stat-group.mapper.d.ts +4 -0
- package/dist/repositories/mongo/stat-group.mapper.js +27 -0
- package/dist/repositories/mongo/stat-group.repository.d.ts +13 -0
- package/dist/repositories/mongo/stat-group.repository.js +165 -0
- package/package.json +1 -1
package/dist/domain/group.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { GroupStatistics } from "../ports";
|
|
2
1
|
import { ParticipantGroup, PlayerPosition, LanguageType, RatingSource } from "../types";
|
|
3
2
|
export declare class Group {
|
|
4
3
|
private readonly groupId;
|
|
@@ -8,7 +7,6 @@ export declare class Group {
|
|
|
8
7
|
private isMessageLast;
|
|
9
8
|
private language;
|
|
10
9
|
private isPublic;
|
|
11
|
-
private statistics;
|
|
12
10
|
private constructor();
|
|
13
11
|
static create(params: {
|
|
14
12
|
groupId: number;
|
|
@@ -18,7 +16,6 @@ export declare class Group {
|
|
|
18
16
|
isMessageLast?: boolean;
|
|
19
17
|
language?: LanguageType;
|
|
20
18
|
isPublic?: boolean;
|
|
21
|
-
statistics?: GroupStatistics;
|
|
22
19
|
}): Group;
|
|
23
20
|
getGroupId(): number;
|
|
24
21
|
getGroupName(): string;
|
|
@@ -27,7 +24,6 @@ export declare class Group {
|
|
|
27
24
|
getLanguage(): LanguageType;
|
|
28
25
|
getParticipants(): ParticipantGroup[];
|
|
29
26
|
getIsPublic(): boolean;
|
|
30
|
-
getStatistics(): GroupStatistics;
|
|
31
27
|
getAdmins(): ParticipantGroup[];
|
|
32
28
|
getAdminIds(): number[];
|
|
33
29
|
setTimezone(tz: string): void;
|
package/dist/domain/group.js
CHANGED
|
@@ -4,7 +4,7 @@ exports.Group = void 0;
|
|
|
4
4
|
const types_1 = require("../types");
|
|
5
5
|
const utils_1 = require("../utils");
|
|
6
6
|
class Group {
|
|
7
|
-
constructor(groupId, groupName, participants, timezone = "UTC+0", isMessageLast = false, language = "en", isPublic = false
|
|
7
|
+
constructor(groupId, groupName, participants, timezone = "UTC+0", isMessageLast = false, language = "en", isPublic = false) {
|
|
8
8
|
this.groupId = groupId;
|
|
9
9
|
this.groupName = groupName;
|
|
10
10
|
this.participants = participants;
|
|
@@ -12,12 +12,11 @@ class Group {
|
|
|
12
12
|
this.isMessageLast = isMessageLast;
|
|
13
13
|
this.language = language;
|
|
14
14
|
this.isPublic = isPublic;
|
|
15
|
-
this.statistics = statistics;
|
|
16
15
|
}
|
|
17
16
|
static create(params) {
|
|
18
17
|
if (!params.groupName)
|
|
19
18
|
throw new Error("Group name is required");
|
|
20
|
-
return new Group(params.groupId, params.groupName, (params.participants ?? []).map(p => ({ ...p, ratingHistory: [...p.ratingHistory] })), params.timezone ?? "UTC+0", params.isMessageLast ?? false, params.language ?? "en", params.isPublic ?? false
|
|
19
|
+
return new Group(params.groupId, params.groupName, (params.participants ?? []).map(p => ({ ...p, ratingHistory: [...p.ratingHistory] })), params.timezone ?? "UTC+0", params.isMessageLast ?? false, params.language ?? "en", params.isPublic ?? false);
|
|
21
20
|
}
|
|
22
21
|
getGroupId() { return this.groupId; }
|
|
23
22
|
getGroupName() { return this.groupName; }
|
|
@@ -26,7 +25,6 @@ class Group {
|
|
|
26
25
|
getLanguage() { return this.language; }
|
|
27
26
|
getParticipants() { return this.participants; }
|
|
28
27
|
getIsPublic() { return this.isPublic; }
|
|
29
|
-
getStatistics() { return this.statistics; }
|
|
30
28
|
getAdmins() {
|
|
31
29
|
return this.participants.filter(p => p.isAdmin);
|
|
32
30
|
}
|
package/dist/domain/index.d.ts
CHANGED
package/dist/domain/index.js
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export declare class StatGroup {
|
|
2
|
+
private readonly groupId;
|
|
3
|
+
private readonly groupName;
|
|
4
|
+
private readonly seasonsCount;
|
|
5
|
+
private readonly eventsCount;
|
|
6
|
+
private readonly matchesCount;
|
|
7
|
+
private readonly participantsCount;
|
|
8
|
+
private readonly updatedAt;
|
|
9
|
+
private constructor();
|
|
10
|
+
static create(params: {
|
|
11
|
+
groupId: number;
|
|
12
|
+
groupName: string;
|
|
13
|
+
seasonsCount: number;
|
|
14
|
+
eventsCount: number;
|
|
15
|
+
matchesCount: number;
|
|
16
|
+
participantsCount: number;
|
|
17
|
+
updatedAt?: Date;
|
|
18
|
+
}): StatGroup;
|
|
19
|
+
getGroupId(): number;
|
|
20
|
+
getGroupName(): string;
|
|
21
|
+
getSeasonsCount(): number;
|
|
22
|
+
getEventsCount(): number;
|
|
23
|
+
getMatchesCount(): number;
|
|
24
|
+
getParticipantsCount(): number;
|
|
25
|
+
getUpdatedAt(): Date;
|
|
26
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StatGroup = void 0;
|
|
4
|
+
class StatGroup {
|
|
5
|
+
constructor(groupId, groupName, seasonsCount, eventsCount, matchesCount, participantsCount, updatedAt) {
|
|
6
|
+
this.groupId = groupId;
|
|
7
|
+
this.groupName = groupName;
|
|
8
|
+
this.seasonsCount = seasonsCount;
|
|
9
|
+
this.eventsCount = eventsCount;
|
|
10
|
+
this.matchesCount = matchesCount;
|
|
11
|
+
this.participantsCount = participantsCount;
|
|
12
|
+
this.updatedAt = updatedAt;
|
|
13
|
+
}
|
|
14
|
+
static create(params) {
|
|
15
|
+
if (!params.groupName)
|
|
16
|
+
throw new Error("Group name is required");
|
|
17
|
+
if (params.seasonsCount < 0)
|
|
18
|
+
throw new Error("Seasons count cannot be negative");
|
|
19
|
+
if (params.eventsCount < 0)
|
|
20
|
+
throw new Error("Events count cannot be negative");
|
|
21
|
+
if (params.matchesCount < 0)
|
|
22
|
+
throw new Error("Matches count cannot be negative");
|
|
23
|
+
if (params.participantsCount < 0)
|
|
24
|
+
throw new Error("Participants count cannot be negative");
|
|
25
|
+
return new StatGroup(params.groupId, params.groupName, params.seasonsCount, params.eventsCount, params.matchesCount, params.participantsCount, params.updatedAt ?? new Date());
|
|
26
|
+
}
|
|
27
|
+
getGroupId() { return this.groupId; }
|
|
28
|
+
getGroupName() { return this.groupName; }
|
|
29
|
+
getSeasonsCount() { return this.seasonsCount; }
|
|
30
|
+
getEventsCount() { return this.eventsCount; }
|
|
31
|
+
getMatchesCount() { return this.matchesCount; }
|
|
32
|
+
getParticipantsCount() { return this.participantsCount; }
|
|
33
|
+
getUpdatedAt() { return this.updatedAt; }
|
|
34
|
+
}
|
|
35
|
+
exports.StatGroup = StatGroup;
|
package/dist/mongo/index.d.ts
CHANGED
package/dist/mongo/index.js
CHANGED
|
@@ -18,3 +18,4 @@ __exportStar(require("./schemas/user.schema"), exports);
|
|
|
18
18
|
__exportStar(require("./schemas/event-template.schema"), exports);
|
|
19
19
|
__exportStar(require("./schemas/event.schema"), exports);
|
|
20
20
|
__exportStar(require("./schemas/group.schema"), exports);
|
|
21
|
+
__exportStar(require("./schemas/stat-group.schema"), exports);
|
|
@@ -10,12 +10,6 @@ export interface GroupDoc {
|
|
|
10
10
|
isPublic?: boolean;
|
|
11
11
|
createdAt?: Date;
|
|
12
12
|
updatedAt?: Date;
|
|
13
|
-
statistics?: {
|
|
14
|
-
seasonsCount: number;
|
|
15
|
-
eventsCount: number;
|
|
16
|
-
matchesCount: number;
|
|
17
|
-
participantsCount: number;
|
|
18
|
-
};
|
|
19
13
|
}
|
|
20
14
|
export declare function createRatingPointSchema(): Schema<RatingPoint, import("mongoose").Model<RatingPoint, any, any, any, import("mongoose").Document<unknown, any, RatingPoint> & RatingPoint & {
|
|
21
15
|
_id: import("mongoose").Types.ObjectId;
|
|
@@ -31,12 +31,6 @@ function createGroupSchema() {
|
|
|
31
31
|
isMessageLast: { type: Boolean, default: false },
|
|
32
32
|
language: { type: String, enum: ['en', 'ru', 'ua'], default: 'en' },
|
|
33
33
|
isPublic: { type: Boolean, default: false, index: true },
|
|
34
|
-
statistics: {
|
|
35
|
-
seasonsCount: { type: Number, default: 0 },
|
|
36
|
-
eventsCount: { type: Number, default: 0 },
|
|
37
|
-
matchesCount: { type: Number, default: 0 },
|
|
38
|
-
participantsCount: { type: Number, default: 0 },
|
|
39
|
-
},
|
|
40
34
|
}, {
|
|
41
35
|
timestamps: true, // Adds createdAt and updatedAt automatically
|
|
42
36
|
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Schema } from "mongoose";
|
|
2
|
+
export interface StatGroupDoc {
|
|
3
|
+
groupId: number;
|
|
4
|
+
groupName: string;
|
|
5
|
+
seasonsCount: number;
|
|
6
|
+
eventsCount: number;
|
|
7
|
+
matchesCount: number;
|
|
8
|
+
participantsCount: number;
|
|
9
|
+
updatedAt: Date;
|
|
10
|
+
}
|
|
11
|
+
export declare function createStatGroupSchema(): Schema<StatGroupDoc, import("mongoose").Model<StatGroupDoc, any, any, any, import("mongoose").Document<unknown, any, StatGroupDoc> & StatGroupDoc & {
|
|
12
|
+
_id: import("mongoose").Types.ObjectId;
|
|
13
|
+
}, any>, {}, {}, {}, {}, import("mongoose").DefaultSchemaOptions, StatGroupDoc, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<StatGroupDoc>> & import("mongoose").FlatRecord<StatGroupDoc> & {
|
|
14
|
+
_id: import("mongoose").Types.ObjectId;
|
|
15
|
+
}>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createStatGroupSchema = createStatGroupSchema;
|
|
4
|
+
const mongoose_1 = require("mongoose");
|
|
5
|
+
function createStatGroupSchema() {
|
|
6
|
+
const StatGroupSchema = new mongoose_1.Schema({
|
|
7
|
+
groupId: { type: Number, required: true, unique: true, index: true },
|
|
8
|
+
groupName: { type: String, required: true },
|
|
9
|
+
seasonsCount: { type: Number, required: true, min: 0 },
|
|
10
|
+
eventsCount: { type: Number, required: true, min: 0 },
|
|
11
|
+
matchesCount: { type: Number, required: true, min: 0 },
|
|
12
|
+
participantsCount: { type: Number, required: true, min: 0 },
|
|
13
|
+
updatedAt: { type: Date, default: Date.now },
|
|
14
|
+
}, {
|
|
15
|
+
collection: 'stat_group',
|
|
16
|
+
versionKey: false, // убирает поле __v
|
|
17
|
+
});
|
|
18
|
+
return StatGroupSchema;
|
|
19
|
+
}
|
|
@@ -13,50 +13,11 @@ export type GroupWrite = {
|
|
|
13
13
|
isMessageLast: boolean;
|
|
14
14
|
language: LanguageType;
|
|
15
15
|
isPublic: boolean;
|
|
16
|
-
statistics?: {
|
|
17
|
-
seasonsCount: number;
|
|
18
|
-
eventsCount: number;
|
|
19
|
-
matchesCount: number;
|
|
20
|
-
participantsCount: number;
|
|
21
|
-
};
|
|
22
16
|
};
|
|
23
|
-
export interface FindPublicGroupsOptions {
|
|
24
|
-
page: number;
|
|
25
|
-
limit: number;
|
|
26
|
-
search?: string;
|
|
27
|
-
sort: 'lastActivity' | 'name' | 'members' | 'games';
|
|
28
|
-
}
|
|
29
|
-
export interface GroupStatistics {
|
|
30
|
-
seasonsCount: number;
|
|
31
|
-
eventsCount: number;
|
|
32
|
-
matchesCount: number;
|
|
33
|
-
participantsCount: number;
|
|
34
|
-
}
|
|
35
|
-
export interface PublicGroupsResult {
|
|
36
|
-
groups: Array<{
|
|
37
|
-
groupId: number;
|
|
38
|
-
groupName: string;
|
|
39
|
-
updatedAt: Date;
|
|
40
|
-
statistics: GroupStatistics;
|
|
41
|
-
}>;
|
|
42
|
-
totalCount: number;
|
|
43
|
-
}
|
|
44
17
|
export interface GroupsRepository {
|
|
45
18
|
create(groupData: Group): Promise<Group>;
|
|
46
19
|
findById(groupId: number): Promise<Group | null>;
|
|
47
20
|
update(groupId: number, updates: Group): Promise<Group | null>;
|
|
48
21
|
findAll(): Promise<Group[]>;
|
|
49
22
|
delete(groupId: number): Promise<void>;
|
|
50
|
-
/**
|
|
51
|
-
* Find public groups with pagination, search, and sorting
|
|
52
|
-
* @param options Query options for filtering, pagination, and sorting
|
|
53
|
-
* @returns Paginated list of public groups with statistics
|
|
54
|
-
*/
|
|
55
|
-
findPublicGroups(options: FindPublicGroupsOptions): Promise<PublicGroupsResult>;
|
|
56
|
-
/**
|
|
57
|
-
* Update statistics for a specific group
|
|
58
|
-
* @param groupId The group ID
|
|
59
|
-
* @param updates Partial statistics to update
|
|
60
|
-
*/
|
|
61
|
-
updateStatistics(groupId: number, updates: Partial<GroupStatistics>): Promise<void>;
|
|
62
23
|
}
|
package/dist/ports/index.d.ts
CHANGED
package/dist/ports/index.js
CHANGED
|
@@ -18,3 +18,4 @@ __exportStar(require("./users.repository"), exports);
|
|
|
18
18
|
__exportStar(require("./groups.repository"), exports);
|
|
19
19
|
__exportStar(require("./events.repository"), exports);
|
|
20
20
|
__exportStar(require("./event-templates.repository"), exports);
|
|
21
|
+
__exportStar(require("./stat-group.repository"), exports);
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { StatGroup } from "../domain/stat-group";
|
|
2
|
+
export interface FindPublicGroupsOptions {
|
|
3
|
+
page: number;
|
|
4
|
+
limit: number;
|
|
5
|
+
search?: string;
|
|
6
|
+
sort: 'lastActivity' | 'name' | 'members' | 'games';
|
|
7
|
+
}
|
|
8
|
+
export interface PublicGroupsWithStatsResult {
|
|
9
|
+
groups: Array<{
|
|
10
|
+
groupId: number;
|
|
11
|
+
groupName: string;
|
|
12
|
+
updatedAt: Date;
|
|
13
|
+
statistics: {
|
|
14
|
+
seasonsCount: number;
|
|
15
|
+
eventsCount: number;
|
|
16
|
+
matchesCount: number;
|
|
17
|
+
participantsCount: number;
|
|
18
|
+
};
|
|
19
|
+
}>;
|
|
20
|
+
totalCount: number;
|
|
21
|
+
}
|
|
22
|
+
export interface StatGroupRepository {
|
|
23
|
+
/**
|
|
24
|
+
* Find statistics for a specific group
|
|
25
|
+
* @param groupId The group ID
|
|
26
|
+
* @returns Group statistics or null if not found
|
|
27
|
+
*/
|
|
28
|
+
findByGroupId(groupId: number): Promise<StatGroup | null>;
|
|
29
|
+
/**
|
|
30
|
+
* Find public groups with their statistics using aggregation
|
|
31
|
+
* @param options Query options for filtering, pagination, and sorting
|
|
32
|
+
* @returns Paginated list of public groups with statistics
|
|
33
|
+
*/
|
|
34
|
+
findPublicGroupsWithStats(options: FindPublicGroupsOptions): Promise<PublicGroupsWithStatsResult>;
|
|
35
|
+
/**
|
|
36
|
+
* Create or update group statistics
|
|
37
|
+
* @param stats The statistics to save
|
|
38
|
+
* @returns The saved statistics
|
|
39
|
+
*/
|
|
40
|
+
upsert(stats: StatGroup): Promise<StatGroup>;
|
|
41
|
+
}
|
|
@@ -18,3 +18,4 @@ __exportStar(require("./mongo/user.repository"), exports);
|
|
|
18
18
|
__exportStar(require("./mongo/group.repository"), exports);
|
|
19
19
|
__exportStar(require("./mongo/event.repository"), exports);
|
|
20
20
|
__exportStar(require("./mongo/event-template.repository"), exports);
|
|
21
|
+
__exportStar(require("./mongo/stat-group.repository"), exports);
|
|
@@ -12,7 +12,6 @@ function toDomain(doc) {
|
|
|
12
12
|
isMessageLast: doc.isMessageLast,
|
|
13
13
|
language: doc.language,
|
|
14
14
|
isPublic: doc.isPublic ?? false,
|
|
15
|
-
statistics: doc.statistics ?? { seasonsCount: 0, eventsCount: 0, matchesCount: 0, participantsCount: 0 },
|
|
16
15
|
});
|
|
17
16
|
}
|
|
18
17
|
function toDoc(group) {
|
|
@@ -24,6 +23,5 @@ function toDoc(group) {
|
|
|
24
23
|
isMessageLast: group.getIsMessageLast(),
|
|
25
24
|
language: group.getLanguage(),
|
|
26
25
|
isPublic: group.getIsPublic(),
|
|
27
|
-
statistics: group.getStatistics(),
|
|
28
26
|
};
|
|
29
27
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Model } from "mongoose";
|
|
2
|
-
import type { GroupsRepository
|
|
2
|
+
import type { GroupsRepository } from "../../ports/groups.repository";
|
|
3
3
|
import type { GroupDoc } from "../../mongo";
|
|
4
4
|
import { Group } from "../../domain/group";
|
|
5
5
|
export declare class MongoGroupsRepository implements GroupsRepository {
|
|
@@ -10,6 +10,4 @@ export declare class MongoGroupsRepository implements GroupsRepository {
|
|
|
10
10
|
update(groupId: number, updates: Group): Promise<Group | null>;
|
|
11
11
|
findAll(): Promise<Group[]>;
|
|
12
12
|
delete(groupId: number): Promise<void>;
|
|
13
|
-
findPublicGroups(options: FindPublicGroupsOptions): Promise<PublicGroupsResult>;
|
|
14
|
-
updateStatistics(groupId: number, updates: Partial<GroupStatistics>): Promise<void>;
|
|
15
13
|
}
|
|
@@ -96,98 +96,5 @@ class MongoGroupsRepository {
|
|
|
96
96
|
throw new errors_1.QueryError('delete', 'Group', groupId, error);
|
|
97
97
|
}
|
|
98
98
|
}
|
|
99
|
-
async findPublicGroups(options) {
|
|
100
|
-
try {
|
|
101
|
-
const { page, limit, search, sort } = options;
|
|
102
|
-
const skip = (page - 1) * limit;
|
|
103
|
-
// Build query
|
|
104
|
-
const query = { isPublic: true };
|
|
105
|
-
// Add search filter if provided
|
|
106
|
-
if (search) {
|
|
107
|
-
query.groupName = { $regex: search, $options: 'i' }; // Case-insensitive search
|
|
108
|
-
}
|
|
109
|
-
// Build sort object based on sort parameter
|
|
110
|
-
let sortObj = {};
|
|
111
|
-
switch (sort) {
|
|
112
|
-
case 'lastActivity':
|
|
113
|
-
sortObj = { updatedAt: -1 }; // Descending (newest first)
|
|
114
|
-
break;
|
|
115
|
-
case 'name':
|
|
116
|
-
sortObj = { groupName: 1 }; // Ascending (A-Z)
|
|
117
|
-
break;
|
|
118
|
-
case 'members':
|
|
119
|
-
// Sort by participants array length
|
|
120
|
-
// Note: This is approximate - for exact sorting, use aggregation pipeline
|
|
121
|
-
sortObj = { participants: -1 }; // Descending (most members first)
|
|
122
|
-
break;
|
|
123
|
-
case 'games':
|
|
124
|
-
// Sort by denormalized matchesCount
|
|
125
|
-
sortObj = { 'statistics.matchesCount': -1 }; // Descending (most games first)
|
|
126
|
-
break;
|
|
127
|
-
}
|
|
128
|
-
// Execute query
|
|
129
|
-
const groups = await this.GroupModel
|
|
130
|
-
.find(query)
|
|
131
|
-
.select('groupId groupName updatedAt participants statistics')
|
|
132
|
-
.sort(sortObj)
|
|
133
|
-
.skip(skip)
|
|
134
|
-
.limit(limit)
|
|
135
|
-
.lean()
|
|
136
|
-
.exec();
|
|
137
|
-
const totalCount = await this.GroupModel.countDocuments(query);
|
|
138
|
-
return {
|
|
139
|
-
groups: groups.map(g => ({
|
|
140
|
-
groupId: g.groupId,
|
|
141
|
-
groupName: g.groupName,
|
|
142
|
-
updatedAt: g.updatedAt, // guaranteed to exist with timestamps: true
|
|
143
|
-
statistics: {
|
|
144
|
-
seasonsCount: g.statistics?.seasonsCount ?? 0,
|
|
145
|
-
eventsCount: g.statistics?.eventsCount ?? 0,
|
|
146
|
-
matchesCount: g.statistics?.matchesCount ?? 0,
|
|
147
|
-
participantsCount: g.participants.length,
|
|
148
|
-
},
|
|
149
|
-
})),
|
|
150
|
-
totalCount,
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
catch (error) {
|
|
154
|
-
throw new errors_1.QueryError('findPublicGroups', 'Group', 'public', error);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
async updateStatistics(groupId, updates) {
|
|
158
|
-
try {
|
|
159
|
-
// Validate input
|
|
160
|
-
if (!groupId || groupId === 0) {
|
|
161
|
-
throw new errors_1.ValidationError('Group', 'groupId', groupId, 'must be a non-zero number');
|
|
162
|
-
}
|
|
163
|
-
if (!updates || Object.keys(updates).length === 0) {
|
|
164
|
-
throw new errors_1.ValidationError('Group', 'updates', updates, 'updates object cannot be empty');
|
|
165
|
-
}
|
|
166
|
-
// Build update object with proper nesting
|
|
167
|
-
const updateObj = {};
|
|
168
|
-
if (updates.seasonsCount !== undefined) {
|
|
169
|
-
updateObj['statistics.seasonsCount'] = updates.seasonsCount;
|
|
170
|
-
}
|
|
171
|
-
if (updates.eventsCount !== undefined) {
|
|
172
|
-
updateObj['statistics.eventsCount'] = updates.eventsCount;
|
|
173
|
-
}
|
|
174
|
-
if (updates.matchesCount !== undefined) {
|
|
175
|
-
updateObj['statistics.matchesCount'] = updates.matchesCount;
|
|
176
|
-
}
|
|
177
|
-
if (updates.participantsCount !== undefined) {
|
|
178
|
-
updateObj['statistics.participantsCount'] = updates.participantsCount;
|
|
179
|
-
}
|
|
180
|
-
const result = await this.GroupModel.updateOne({ groupId }, { $set: updateObj });
|
|
181
|
-
if (result.matchedCount === 0) {
|
|
182
|
-
throw new errors_1.EntityNotFoundError('Group', groupId, 'updateStatistics');
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
catch (error) {
|
|
186
|
-
if (error instanceof errors_1.ValidationError || error instanceof errors_1.EntityNotFoundError) {
|
|
187
|
-
throw error;
|
|
188
|
-
}
|
|
189
|
-
throw new errors_1.QueryError('updateStatistics', 'Group', groupId, error);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
99
|
}
|
|
193
100
|
exports.MongoGroupsRepository = MongoGroupsRepository;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toStatGroupDomain = toStatGroupDomain;
|
|
4
|
+
exports.toStatGroupDoc = toStatGroupDoc;
|
|
5
|
+
const stat_group_1 = require("../../domain/stat-group");
|
|
6
|
+
function toStatGroupDomain(doc) {
|
|
7
|
+
return stat_group_1.StatGroup.create({
|
|
8
|
+
groupId: doc.groupId,
|
|
9
|
+
groupName: doc.groupName,
|
|
10
|
+
seasonsCount: doc.seasonsCount,
|
|
11
|
+
eventsCount: doc.eventsCount,
|
|
12
|
+
matchesCount: doc.matchesCount,
|
|
13
|
+
participantsCount: doc.participantsCount,
|
|
14
|
+
updatedAt: doc.updatedAt,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
function toStatGroupDoc(statGroup) {
|
|
18
|
+
return {
|
|
19
|
+
groupId: statGroup.getGroupId(),
|
|
20
|
+
groupName: statGroup.getGroupName(),
|
|
21
|
+
seasonsCount: statGroup.getSeasonsCount(),
|
|
22
|
+
eventsCount: statGroup.getEventsCount(),
|
|
23
|
+
matchesCount: statGroup.getMatchesCount(),
|
|
24
|
+
participantsCount: statGroup.getParticipantsCount(),
|
|
25
|
+
updatedAt: statGroup.getUpdatedAt(),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Model } from "mongoose";
|
|
2
|
+
import type { StatGroupRepository, FindPublicGroupsOptions, PublicGroupsWithStatsResult } from "../../ports/stat-group.repository";
|
|
3
|
+
import type { StatGroupDoc } from "../../mongo";
|
|
4
|
+
import type { GroupDoc } from "../../mongo";
|
|
5
|
+
import { StatGroup } from "../../domain/stat-group";
|
|
6
|
+
export declare class MongoStatGroupRepository implements StatGroupRepository {
|
|
7
|
+
private readonly StatGroupModel;
|
|
8
|
+
private readonly GroupModel;
|
|
9
|
+
constructor(StatGroupModel: Model<StatGroupDoc>, GroupModel: Model<GroupDoc>);
|
|
10
|
+
findByGroupId(groupId: number): Promise<StatGroup | null>;
|
|
11
|
+
findPublicGroupsWithStats(options: FindPublicGroupsOptions): Promise<PublicGroupsWithStatsResult>;
|
|
12
|
+
upsert(stats: StatGroup): Promise<StatGroup>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MongoStatGroupRepository = void 0;
|
|
4
|
+
const stat_group_mapper_1 = require("./stat-group.mapper");
|
|
5
|
+
const errors_1 = require("../../errors");
|
|
6
|
+
class MongoStatGroupRepository {
|
|
7
|
+
constructor(StatGroupModel, GroupModel) {
|
|
8
|
+
this.StatGroupModel = StatGroupModel;
|
|
9
|
+
this.GroupModel = GroupModel;
|
|
10
|
+
}
|
|
11
|
+
async findByGroupId(groupId) {
|
|
12
|
+
try {
|
|
13
|
+
// Validate input
|
|
14
|
+
if (!groupId || groupId === 0) {
|
|
15
|
+
throw new errors_1.ValidationError('StatGroup', 'groupId', groupId, 'must be a non-zero number');
|
|
16
|
+
}
|
|
17
|
+
const doc = await this.StatGroupModel.findOne({ groupId }).lean();
|
|
18
|
+
return doc ? (0, stat_group_mapper_1.toStatGroupDomain)(doc) : null;
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
if (error instanceof errors_1.ValidationError) {
|
|
22
|
+
throw error;
|
|
23
|
+
}
|
|
24
|
+
throw new errors_1.QueryError('findByGroupId', 'StatGroup', groupId, error);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
async findPublicGroupsWithStats(options) {
|
|
28
|
+
try {
|
|
29
|
+
const { page, limit, search, sort } = options;
|
|
30
|
+
const skip = (page - 1) * limit;
|
|
31
|
+
// Build match stage for public groups
|
|
32
|
+
const matchStage = { isPublic: true };
|
|
33
|
+
if (search) {
|
|
34
|
+
matchStage.groupName = { $regex: search, $options: 'i' };
|
|
35
|
+
}
|
|
36
|
+
// Build sort stage
|
|
37
|
+
let sortStage = {};
|
|
38
|
+
switch (sort) {
|
|
39
|
+
case 'lastActivity':
|
|
40
|
+
sortStage = { 'statistics.updatedAt': -1 };
|
|
41
|
+
break;
|
|
42
|
+
case 'name':
|
|
43
|
+
sortStage = { groupName: 1 };
|
|
44
|
+
break;
|
|
45
|
+
case 'members':
|
|
46
|
+
sortStage = { 'statistics.participantsCount': -1 };
|
|
47
|
+
break;
|
|
48
|
+
case 'games':
|
|
49
|
+
sortStage = { 'statistics.matchesCount': -1 };
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
// Aggregation pipeline to join groups with statistics
|
|
53
|
+
const pipeline = [
|
|
54
|
+
// Match public groups
|
|
55
|
+
{ $match: matchStage },
|
|
56
|
+
// Lookup statistics
|
|
57
|
+
{
|
|
58
|
+
$lookup: {
|
|
59
|
+
from: 'stat_group',
|
|
60
|
+
localField: 'groupId',
|
|
61
|
+
foreignField: 'groupId',
|
|
62
|
+
as: 'statistics'
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
// Unwind statistics (handle case where no stats exist)
|
|
66
|
+
{
|
|
67
|
+
$unwind: {
|
|
68
|
+
path: '$statistics',
|
|
69
|
+
preserveNullAndEmptyArrays: true
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
// Add default statistics if none exist
|
|
73
|
+
{
|
|
74
|
+
$addFields: {
|
|
75
|
+
statistics: {
|
|
76
|
+
$cond: {
|
|
77
|
+
if: { $eq: ['$statistics', null] },
|
|
78
|
+
then: {
|
|
79
|
+
seasonsCount: 0,
|
|
80
|
+
eventsCount: 0,
|
|
81
|
+
matchesCount: 0,
|
|
82
|
+
participantsCount: { $size: '$participants' },
|
|
83
|
+
updatedAt: '$updatedAt'
|
|
84
|
+
},
|
|
85
|
+
else: {
|
|
86
|
+
seasonsCount: '$statistics.seasonsCount',
|
|
87
|
+
eventsCount: '$statistics.eventsCount',
|
|
88
|
+
matchesCount: '$statistics.matchesCount',
|
|
89
|
+
participantsCount: '$statistics.participantsCount',
|
|
90
|
+
updatedAt: '$statistics.updatedAt'
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
// Sort
|
|
97
|
+
{ $sort: sortStage },
|
|
98
|
+
// Pagination
|
|
99
|
+
{ $skip: skip },
|
|
100
|
+
{ $limit: limit },
|
|
101
|
+
// Project final result
|
|
102
|
+
{
|
|
103
|
+
$project: {
|
|
104
|
+
groupId: 1,
|
|
105
|
+
groupName: 1,
|
|
106
|
+
updatedAt: 1,
|
|
107
|
+
statistics: 1
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
];
|
|
111
|
+
// Execute aggregation
|
|
112
|
+
const groups = await this.GroupModel.aggregate(pipeline).exec();
|
|
113
|
+
// Get total count for pagination
|
|
114
|
+
const totalCountPipeline = [
|
|
115
|
+
{ $match: matchStage },
|
|
116
|
+
{ $count: 'total' }
|
|
117
|
+
];
|
|
118
|
+
const totalResult = await this.GroupModel.aggregate(totalCountPipeline).exec();
|
|
119
|
+
const totalCount = totalResult[0]?.total || 0;
|
|
120
|
+
return {
|
|
121
|
+
groups: groups.map(g => ({
|
|
122
|
+
groupId: g.groupId,
|
|
123
|
+
groupName: g.groupName,
|
|
124
|
+
updatedAt: g.updatedAt,
|
|
125
|
+
statistics: g.statistics,
|
|
126
|
+
})),
|
|
127
|
+
totalCount,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
throw new errors_1.QueryError('findPublicGroupsWithStats', 'StatGroup', 'public', error);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
async upsert(stats) {
|
|
135
|
+
try {
|
|
136
|
+
// Validate input
|
|
137
|
+
if (!stats) {
|
|
138
|
+
throw new errors_1.ValidationError('StatGroup', 'stats', stats, 'stats object is required');
|
|
139
|
+
}
|
|
140
|
+
const groupId = stats.getGroupId();
|
|
141
|
+
if (!groupId || groupId === 0) {
|
|
142
|
+
throw new errors_1.ValidationError('StatGroup', 'groupId', groupId, 'must be a non-zero number');
|
|
143
|
+
}
|
|
144
|
+
const doc = (0, stat_group_mapper_1.toStatGroupDoc)(stats);
|
|
145
|
+
const result = await this.StatGroupModel.findOneAndUpdate({ groupId }, {
|
|
146
|
+
$set: {
|
|
147
|
+
...doc,
|
|
148
|
+
updatedAt: new Date(),
|
|
149
|
+
}
|
|
150
|
+
}, {
|
|
151
|
+
upsert: true,
|
|
152
|
+
new: true,
|
|
153
|
+
setDefaultsOnInsert: true,
|
|
154
|
+
}).lean();
|
|
155
|
+
return (0, stat_group_mapper_1.toStatGroupDomain)(result);
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
if (error instanceof errors_1.ValidationError) {
|
|
159
|
+
throw error;
|
|
160
|
+
}
|
|
161
|
+
throw new errors_1.QueryError('upsert', 'StatGroup', stats.getGroupId(), error);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
exports.MongoStatGroupRepository = MongoStatGroupRepository;
|