@aneuhold/be-ts-db-lib 4.2.22 → 4.2.24
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/CHANGELOG.md +19 -0
- package/lib/repositories/workout/WorkoutExerciseRepository.d.ts +5 -2
- package/lib/repositories/workout/WorkoutExerciseRepository.d.ts.map +1 -1
- package/lib/repositories/workout/WorkoutExerciseRepository.js +107 -88
- package/lib/repositories/workout/WorkoutExerciseRepository.js.map +1 -1
- package/lib/repositories/workout/WorkoutExerciseRepository.ts +63 -46
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,23 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## 🔖 [4.2.24] (2026-04-17)
|
|
9
|
+
|
|
10
|
+
### 🏗️ Changed
|
|
11
|
+
|
|
12
|
+
- Updated dependencies on `@aneuhold/be-ts-lib` to `^3.1.11` and `@aneuhold/core-ts-db-lib` to `^5.0.6`.
|
|
13
|
+
|
|
14
|
+
## 🔖 [4.2.23] (2026-04-15)
|
|
15
|
+
|
|
16
|
+
### ✅ Added
|
|
17
|
+
|
|
18
|
+
- `WorkoutExerciseRepository.buildExerciseCTOs` now runs a third parallel aggregation pipeline to populate `lastSessionExercise`/`lastSessionSets` from the true latest completed session (any cycle type, including deloads and free-form).
|
|
19
|
+
|
|
20
|
+
### 🏗️ Changed
|
|
21
|
+
|
|
22
|
+
- Repository now populates `lastAccumulationSessionExercise`/`lastAccumulationSessionSets` alongside `lastSessionExercise`/`lastSessionSets` in each `WorkoutExerciseCTO`.
|
|
23
|
+
- Updated dependencies on `@aneuhold/be-ts-lib` to `^3.1.10` and `@aneuhold/core-ts-db-lib` to `^5.0.5`.
|
|
24
|
+
|
|
8
25
|
## 🔖 [4.2.22] (2026-03-26)
|
|
9
26
|
|
|
10
27
|
### 🏗️ Changed
|
|
@@ -378,6 +395,8 @@ Updated dependencies: now requires `@aneuhold/core-ts-db-lib@^3.0.0`, `@aneuhold
|
|
|
378
395
|
- Updated workflow permissions to allow repository write access
|
|
379
396
|
|
|
380
397
|
<!-- Link References -->
|
|
398
|
+
[4.2.24]: https://github.com/aneuhold/ts-libs/compare/be-ts-db-lib-v4.2.23...be-ts-db-lib-v4.2.24
|
|
399
|
+
[4.2.23]: https://github.com/aneuhold/ts-libs/compare/be-ts-db-lib-v4.2.22...be-ts-db-lib-v4.2.23
|
|
381
400
|
[4.2.22]: https://github.com/aneuhold/ts-libs/compare/be-ts-db-lib-v4.2.21...be-ts-db-lib-v4.2.22
|
|
382
401
|
[4.2.21]: https://github.com/aneuhold/ts-libs/compare/be-ts-db-lib-v4.2.20...be-ts-db-lib-v4.2.21
|
|
383
402
|
[4.2.20]: https://github.com/aneuhold/ts-libs/compare/be-ts-db-lib-v4.2.19...be-ts-db-lib-v4.2.20
|
|
@@ -13,9 +13,12 @@ export default class WorkoutExerciseRepository extends WorkoutBaseWithUserIdRepo
|
|
|
13
13
|
static getRepo(): WorkoutExerciseRepository;
|
|
14
14
|
/**
|
|
15
15
|
* Builds {@link WorkoutExerciseCTO} objects for all exercises belonging to a user.
|
|
16
|
-
* Uses
|
|
16
|
+
* Uses three parallel MongoDB aggregation pipelines:
|
|
17
17
|
* - Pipeline A: exercise + equipmentType + bestCalibration + bestSet
|
|
18
|
-
* - Pipeline B: lastSessionExercise + lastSessionSets
|
|
18
|
+
* - Pipeline B (true latest): lastSessionExercise + lastSessionSets from the most
|
|
19
|
+
* recent completed session, regardless of cycle type / deload status.
|
|
20
|
+
* - Pipeline C (accumulation): lastAccumulationSessionExercise +
|
|
21
|
+
* lastAccumulationSessionSets from the most recent completed non-deload session.
|
|
19
22
|
*
|
|
20
23
|
* @param userId The user whose exercise CTOs to build.
|
|
21
24
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WorkoutExerciseRepository.d.ts","sourceRoot":"","sources":["../../../src/repositories/workout/WorkoutExerciseRepository.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,IAAI,EAEJ,eAAe,EAEf,kBAAkB,EAGnB,MAAM,0BAA0B,CAAC;AAWlC,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"WorkoutExerciseRepository.d.ts","sourceRoot":"","sources":["../../../src/repositories/workout/WorkoutExerciseRepository.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,IAAI,EAEJ,eAAe,EAEf,kBAAkB,EAGnB,MAAM,0BAA0B,CAAC;AAWlC,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAEnC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,2CAA2C,CAAC;AAE/E,OAAO,+BAA+B,MAAM,sCAAsC,CAAC;AAGnF;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,yBAA0B,SAAQ,+BAA+B,CAAC,eAAe,CAAC;IACrG,OAAO,CAAC,MAAM,CAAC,iBAAiB,CAAC,CAA4B;IAE7D,OAAO;IAIP,MAAM,CAAC,uBAAuB,IAAI,aAAa,CAAC,IAAI,CAAC;IAYrD,SAAS,CAAC,gBAAgB,IAAI,IAAI;WAIpB,OAAO,IAAI,yBAAyB;IAOlD;;;;;;;;;;OAUG;IACG,wBAAwB,CAAC,MAAM,EAAE,IAAI,GAAG,OAAO,CAAC,kBAAkB,EAAE,CAAC;CA+N5E"}
|
|
@@ -32,9 +32,12 @@ export default class WorkoutExerciseRepository extends WorkoutBaseWithUserIdRepo
|
|
|
32
32
|
}
|
|
33
33
|
/**
|
|
34
34
|
* Builds {@link WorkoutExerciseCTO} objects for all exercises belonging to a user.
|
|
35
|
-
* Uses
|
|
35
|
+
* Uses three parallel MongoDB aggregation pipelines:
|
|
36
36
|
* - Pipeline A: exercise + equipmentType + bestCalibration + bestSet
|
|
37
|
-
* - Pipeline B: lastSessionExercise + lastSessionSets
|
|
37
|
+
* - Pipeline B (true latest): lastSessionExercise + lastSessionSets from the most
|
|
38
|
+
* recent completed session, regardless of cycle type / deload status.
|
|
39
|
+
* - Pipeline C (accumulation): lastAccumulationSessionExercise +
|
|
40
|
+
* lastAccumulationSessionSets from the most recent completed non-deload session.
|
|
38
41
|
*
|
|
39
42
|
* @param userId The user whose exercise CTOs to build.
|
|
40
43
|
*/
|
|
@@ -114,107 +117,123 @@ export default class WorkoutExerciseRepository extends WorkoutBaseWithUserIdRepo
|
|
|
114
117
|
}
|
|
115
118
|
])
|
|
116
119
|
.toArray();
|
|
117
|
-
//
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
120
|
+
// Builds the pipeline that finds, per exercise, the most recent session
|
|
121
|
+
// exercise from a completed session — optionally restricted to non-deload
|
|
122
|
+
// sessions (where plannedRir is not null on at least one set).
|
|
123
|
+
const buildLastSessionPipeline = (accumulationOnly) => {
|
|
124
|
+
const accumulationFilter = accumulationOnly
|
|
125
|
+
? [
|
|
126
|
+
// Check for non-deload sets (plannedRir is not null)
|
|
127
|
+
{
|
|
128
|
+
$lookup: {
|
|
129
|
+
from: collName,
|
|
130
|
+
let: { seId: '$_se._id' },
|
|
131
|
+
pipeline: [
|
|
132
|
+
{
|
|
133
|
+
$match: {
|
|
134
|
+
$expr: { $eq: ['$workoutSessionExerciseId', '$$seId'] },
|
|
135
|
+
docType: WorkoutSet_docType,
|
|
136
|
+
plannedRir: { $ne: null }
|
|
137
|
+
}
|
|
138
|
+
},
|
|
139
|
+
{ $limit: 1 }
|
|
140
|
+
],
|
|
141
|
+
as: '_nonDeload'
|
|
135
142
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
as: '_nonDeload'
|
|
158
|
-
}
|
|
159
|
-
},
|
|
160
|
-
// Keep only session exercises with at least one non-deload set
|
|
161
|
-
{ $match: { '_nonDeload.0': { $exists: true } } },
|
|
162
|
-
// Per exercise, keep only the most recent session exercise ($first after sort)
|
|
163
|
-
{
|
|
164
|
-
$group: {
|
|
165
|
-
_id: '$_se.workoutExerciseId',
|
|
166
|
-
lastSessionExercise: { $first: '$_se' }
|
|
167
|
-
}
|
|
168
|
-
},
|
|
169
|
-
// Join all sets from the session exercise's setOrder
|
|
170
|
-
{
|
|
171
|
-
$lookup: {
|
|
172
|
-
from: collName,
|
|
173
|
-
let: { setIds: '$lastSessionExercise.setOrder' },
|
|
174
|
-
pipeline: [
|
|
175
|
-
{
|
|
176
|
-
$match: {
|
|
177
|
-
$expr: { $in: ['$_id', '$$setIds'] },
|
|
178
|
-
docType: WorkoutSet_docType
|
|
143
|
+
},
|
|
144
|
+
// Keep only session exercises with at least one non-deload set
|
|
145
|
+
{ $match: { '_nonDeload.0': { $exists: true } } }
|
|
146
|
+
]
|
|
147
|
+
: [];
|
|
148
|
+
return [
|
|
149
|
+
// Start with all completed sessions for this user
|
|
150
|
+
{ $match: { docType: WorkoutSession_docType, userId, complete: true } },
|
|
151
|
+
// Most recent sessions first
|
|
152
|
+
{ $sort: { startTime: -1 } },
|
|
153
|
+
// Join each session's session exercises
|
|
154
|
+
{
|
|
155
|
+
$lookup: {
|
|
156
|
+
from: collName,
|
|
157
|
+
let: { sessId: '$_id' },
|
|
158
|
+
pipeline: [
|
|
159
|
+
{
|
|
160
|
+
$match: {
|
|
161
|
+
$expr: { $eq: ['$workoutSessionId', '$$sessId'] },
|
|
162
|
+
docType: WorkoutSessionExercise_docType
|
|
163
|
+
}
|
|
179
164
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
165
|
+
],
|
|
166
|
+
as: '_se'
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
// One row per session exercise (unwind coalescence optimization)
|
|
170
|
+
{ $unwind: '$_se' },
|
|
171
|
+
...accumulationFilter,
|
|
172
|
+
// Per exercise, keep only the most recent session exercise ($first after sort)
|
|
173
|
+
{
|
|
174
|
+
$group: {
|
|
175
|
+
_id: '$_se.workoutExerciseId',
|
|
176
|
+
lastSessionExercise: { $first: '$_se' }
|
|
177
|
+
}
|
|
178
|
+
},
|
|
179
|
+
// Join all sets from the session exercise's setOrder
|
|
180
|
+
{
|
|
181
|
+
$lookup: {
|
|
182
|
+
from: collName,
|
|
183
|
+
let: { setIds: '$lastSessionExercise.setOrder' },
|
|
184
|
+
pipeline: [
|
|
185
|
+
{
|
|
186
|
+
$match: {
|
|
187
|
+
$expr: { $in: ['$_id', '$$setIds'] },
|
|
188
|
+
docType: WorkoutSet_docType
|
|
189
|
+
}
|
|
190
|
+
},
|
|
191
|
+
// Preserve the original setOrder by sorting on index position
|
|
192
|
+
{
|
|
193
|
+
$addFields: {
|
|
194
|
+
_sortIdx: { $indexOfArray: ['$$setIds', '$_id'] }
|
|
195
|
+
}
|
|
196
|
+
},
|
|
197
|
+
{ $sort: { _sortIdx: 1 } },
|
|
198
|
+
{ $project: { _sortIdx: 0 } }
|
|
199
|
+
],
|
|
200
|
+
as: '_lastSessionSetsArr'
|
|
201
|
+
}
|
|
191
202
|
}
|
|
192
|
-
|
|
193
|
-
|
|
203
|
+
];
|
|
204
|
+
};
|
|
205
|
+
// Pipeline B: most recent session exercise per exercise (any cycle type)
|
|
206
|
+
const pipelineBPromise = collection
|
|
207
|
+
.aggregate(buildLastSessionPipeline(false))
|
|
194
208
|
.toArray();
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
209
|
+
// Pipeline C: most recent accumulation (non-deload) session exercise per exercise
|
|
210
|
+
const pipelineCPromise = collection
|
|
211
|
+
.aggregate(buildLastSessionPipeline(true))
|
|
212
|
+
.toArray();
|
|
213
|
+
const [rawExercises, rawLastSessions, rawLastAccumulationSessions] = await Promise.all([
|
|
214
|
+
pipelineAPromise,
|
|
215
|
+
pipelineBPromise,
|
|
216
|
+
pipelineCPromise
|
|
217
|
+
]);
|
|
218
|
+
const lastSessionMap = new Map(rawLastSessions.map((row) => [row._id, row]));
|
|
219
|
+
const lastAccumulationMap = new Map(rawLastAccumulationSessions.map((row) => [row._id, row]));
|
|
204
220
|
// Assemble and validate CTOs
|
|
205
221
|
return rawExercises.map((raw) => {
|
|
206
222
|
const { _equipArr, _bestCalArr, _bestSetArr, ...exerciseFields } = raw;
|
|
207
223
|
if (_equipArr.length === 0) {
|
|
208
224
|
throw new Error(`Equipment type not found for exercise ${raw._id}`);
|
|
209
225
|
}
|
|
210
|
-
const
|
|
226
|
+
const lastRow = lastSessionMap.get(raw._id);
|
|
227
|
+
const lastAccumulationRow = lastAccumulationMap.get(raw._id);
|
|
211
228
|
return WorkoutExerciseCTOSchema.parse({
|
|
212
229
|
...exerciseFields,
|
|
213
230
|
equipmentType: _equipArr[0],
|
|
214
231
|
bestCalibration: _bestCalArr[0] ?? null,
|
|
215
232
|
bestSet: _bestSetArr[0] ?? null,
|
|
216
|
-
lastSessionExercise:
|
|
217
|
-
lastSessionSets:
|
|
233
|
+
lastSessionExercise: lastRow?.lastSessionExercise ?? null,
|
|
234
|
+
lastSessionSets: lastRow?._lastSessionSetsArr,
|
|
235
|
+
lastAccumulationSessionExercise: lastAccumulationRow?.lastSessionExercise ?? null,
|
|
236
|
+
lastAccumulationSessionSets: lastAccumulationRow?._lastSessionSetsArr
|
|
218
237
|
});
|
|
219
238
|
});
|
|
220
239
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WorkoutExerciseRepository.js","sourceRoot":"","sources":["../../../src/repositories/workout/WorkoutExerciseRepository.ts"],"names":[],"mappings":"AASA,OAAO,EACL,4BAA4B,EAC5B,uBAAuB,EACvB,kCAAkC,EAClC,iCAAiC,EACjC,wBAAwB,EACxB,sBAAsB,EACtB,8BAA8B,EAC9B,kBAAkB,EACnB,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"WorkoutExerciseRepository.js","sourceRoot":"","sources":["../../../src/repositories/workout/WorkoutExerciseRepository.ts"],"names":[],"mappings":"AASA,OAAO,EACL,4BAA4B,EAC5B,uBAAuB,EACvB,kCAAkC,EAClC,iCAAiC,EACjC,wBAAwB,EACxB,sBAAsB,EACtB,8BAA8B,EAC9B,kBAAkB,EACnB,MAAM,0BAA0B,CAAC;AAIlC,OAAO,wBAAwB,MAAM,+CAA+C,CAAC;AACrF,OAAO,+BAA+B,MAAM,sCAAsC,CAAC;AACnF,OAAO,oCAAoC,MAAM,2CAA2C,CAAC;AAE7F;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,yBAA0B,SAAQ,+BAAgD;IAC7F,MAAM,CAAC,iBAAiB,CAA6B;IAE7D;QACE,KAAK,CAAC,uBAAuB,EAAE,IAAI,wBAAwB,EAAE,CAAC,CAAC;IACjE,CAAC;IAED,MAAM,CAAC,uBAAuB;QAC5B,MAAM,YAAY,GAAG,yBAAyB,CAAC,OAAO,EAAE,CAAC;QACzD,OAAO;YACL,SAAS,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE;gBAChC,MAAM,YAAY,CAAC,gBAAgB,CAAC,MAAM,EAAE,IAAI,CAAC,CA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
|
|
18
18
|
WorkoutSet_docType
|
|
19
19
|
} from '@aneuhold/core-ts-db-lib';
|
|
20
20
|
import type { UUID } from 'crypto';
|
|
21
|
+
import type { Document } from 'mongodb';
|
|
21
22
|
import type { RepoListeners } from '../../services/RepoSubscriptionService.js';
|
|
22
23
|
import WorkoutExerciseValidator from '../../validators/workout/ExerciseValidator.js';
|
|
23
24
|
import WorkoutBaseWithUserIdRepository from './WorkoutBaseWithUserIdRepository.js';
|
|
@@ -58,9 +59,12 @@ export default class WorkoutExerciseRepository extends WorkoutBaseWithUserIdRepo
|
|
|
58
59
|
|
|
59
60
|
/**
|
|
60
61
|
* Builds {@link WorkoutExerciseCTO} objects for all exercises belonging to a user.
|
|
61
|
-
* Uses
|
|
62
|
+
* Uses three parallel MongoDB aggregation pipelines:
|
|
62
63
|
* - Pipeline A: exercise + equipmentType + bestCalibration + bestSet
|
|
63
|
-
* - Pipeline B: lastSessionExercise + lastSessionSets
|
|
64
|
+
* - Pipeline B (true latest): lastSessionExercise + lastSessionSets from the most
|
|
65
|
+
* recent completed session, regardless of cycle type / deload status.
|
|
66
|
+
* - Pipeline C (accumulation): lastAccumulationSessionExercise +
|
|
67
|
+
* lastAccumulationSessionSets from the most recent completed non-deload session.
|
|
64
68
|
*
|
|
65
69
|
* @param userId The user whose exercise CTOs to build.
|
|
66
70
|
*/
|
|
@@ -72,8 +76,8 @@ export default class WorkoutExerciseRepository extends WorkoutBaseWithUserIdRepo
|
|
|
72
76
|
_bestSetArr: WorkoutSet[];
|
|
73
77
|
}
|
|
74
78
|
|
|
75
|
-
/** Raw shape of Pipeline B results. */
|
|
76
|
-
interface
|
|
79
|
+
/** Raw shape of Pipeline B/C results. */
|
|
80
|
+
interface LastSessionRow {
|
|
77
81
|
_id: string;
|
|
78
82
|
lastSessionExercise: WorkoutSessionExercise;
|
|
79
83
|
_lastSessionSetsArr: WorkoutSet[];
|
|
@@ -159,9 +163,36 @@ export default class WorkoutExerciseRepository extends WorkoutBaseWithUserIdRepo
|
|
|
159
163
|
])
|
|
160
164
|
.toArray();
|
|
161
165
|
|
|
162
|
-
//
|
|
163
|
-
|
|
164
|
-
|
|
166
|
+
// Builds the pipeline that finds, per exercise, the most recent session
|
|
167
|
+
// exercise from a completed session — optionally restricted to non-deload
|
|
168
|
+
// sessions (where plannedRir is not null on at least one set).
|
|
169
|
+
const buildLastSessionPipeline = (accumulationOnly: boolean): Document[] => {
|
|
170
|
+
const accumulationFilter: Document[] = accumulationOnly
|
|
171
|
+
? [
|
|
172
|
+
// Check for non-deload sets (plannedRir is not null)
|
|
173
|
+
{
|
|
174
|
+
$lookup: {
|
|
175
|
+
from: collName,
|
|
176
|
+
let: { seId: '$_se._id' },
|
|
177
|
+
pipeline: [
|
|
178
|
+
{
|
|
179
|
+
$match: {
|
|
180
|
+
$expr: { $eq: ['$workoutSessionExerciseId', '$$seId'] },
|
|
181
|
+
docType: WorkoutSet_docType,
|
|
182
|
+
plannedRir: { $ne: null }
|
|
183
|
+
}
|
|
184
|
+
},
|
|
185
|
+
{ $limit: 1 }
|
|
186
|
+
],
|
|
187
|
+
as: '_nonDeload'
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
// Keep only session exercises with at least one non-deload set
|
|
191
|
+
{ $match: { '_nonDeload.0': { $exists: true } } }
|
|
192
|
+
]
|
|
193
|
+
: [];
|
|
194
|
+
|
|
195
|
+
return [
|
|
165
196
|
// Start with all completed sessions for this user
|
|
166
197
|
{ $match: { docType: WorkoutSession_docType, userId, complete: true } },
|
|
167
198
|
// Most recent sessions first
|
|
@@ -184,26 +215,7 @@ export default class WorkoutExerciseRepository extends WorkoutBaseWithUserIdRepo
|
|
|
184
215
|
},
|
|
185
216
|
// One row per session exercise (unwind coalescence optimization)
|
|
186
217
|
{ $unwind: '$_se' },
|
|
187
|
-
|
|
188
|
-
{
|
|
189
|
-
$lookup: {
|
|
190
|
-
from: collName,
|
|
191
|
-
let: { seId: '$_se._id' },
|
|
192
|
-
pipeline: [
|
|
193
|
-
{
|
|
194
|
-
$match: {
|
|
195
|
-
$expr: { $eq: ['$workoutSessionExerciseId', '$$seId'] },
|
|
196
|
-
docType: WorkoutSet_docType,
|
|
197
|
-
plannedRir: { $ne: null }
|
|
198
|
-
}
|
|
199
|
-
},
|
|
200
|
-
{ $limit: 1 }
|
|
201
|
-
],
|
|
202
|
-
as: '_nonDeload'
|
|
203
|
-
}
|
|
204
|
-
},
|
|
205
|
-
// Keep only session exercises with at least one non-deload set
|
|
206
|
-
{ $match: { '_nonDeload.0': { $exists: true } } },
|
|
218
|
+
...accumulationFilter,
|
|
207
219
|
// Per exercise, keep only the most recent session exercise ($first after sort)
|
|
208
220
|
{
|
|
209
221
|
$group: {
|
|
@@ -235,25 +247,27 @@ export default class WorkoutExerciseRepository extends WorkoutBaseWithUserIdRepo
|
|
|
235
247
|
as: '_lastSessionSetsArr'
|
|
236
248
|
}
|
|
237
249
|
}
|
|
238
|
-
]
|
|
250
|
+
];
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
// Pipeline B: most recent session exercise per exercise (any cycle type)
|
|
254
|
+
const pipelineBPromise = collection
|
|
255
|
+
.aggregate<LastSessionRow>(buildLastSessionPipeline(false))
|
|
239
256
|
.toArray();
|
|
240
257
|
|
|
241
|
-
|
|
258
|
+
// Pipeline C: most recent accumulation (non-deload) session exercise per exercise
|
|
259
|
+
const pipelineCPromise = collection
|
|
260
|
+
.aggregate<LastSessionRow>(buildLastSessionPipeline(true))
|
|
261
|
+
.toArray();
|
|
242
262
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
for (const row of rawLastSessions) {
|
|
252
|
-
lastSessionMap.set(row._id, {
|
|
253
|
-
lastSessionExercise: row.lastSessionExercise,
|
|
254
|
-
lastSessionSets: row._lastSessionSetsArr
|
|
255
|
-
});
|
|
256
|
-
}
|
|
263
|
+
const [rawExercises, rawLastSessions, rawLastAccumulationSessions] = await Promise.all([
|
|
264
|
+
pipelineAPromise,
|
|
265
|
+
pipelineBPromise,
|
|
266
|
+
pipelineCPromise
|
|
267
|
+
]);
|
|
268
|
+
|
|
269
|
+
const lastSessionMap = new Map(rawLastSessions.map((row) => [row._id, row]));
|
|
270
|
+
const lastAccumulationMap = new Map(rawLastAccumulationSessions.map((row) => [row._id, row]));
|
|
257
271
|
|
|
258
272
|
// Assemble and validate CTOs
|
|
259
273
|
return rawExercises.map((raw) => {
|
|
@@ -262,15 +276,18 @@ export default class WorkoutExerciseRepository extends WorkoutBaseWithUserIdRepo
|
|
|
262
276
|
throw new Error(`Equipment type not found for exercise ${raw._id}`);
|
|
263
277
|
}
|
|
264
278
|
|
|
265
|
-
const
|
|
279
|
+
const lastRow = lastSessionMap.get(raw._id as string);
|
|
280
|
+
const lastAccumulationRow = lastAccumulationMap.get(raw._id as string);
|
|
266
281
|
|
|
267
282
|
return WorkoutExerciseCTOSchema.parse({
|
|
268
283
|
...exerciseFields,
|
|
269
284
|
equipmentType: _equipArr[0],
|
|
270
285
|
bestCalibration: _bestCalArr[0] ?? null,
|
|
271
286
|
bestSet: _bestSetArr[0] ?? null,
|
|
272
|
-
lastSessionExercise:
|
|
273
|
-
lastSessionSets:
|
|
287
|
+
lastSessionExercise: lastRow?.lastSessionExercise ?? null,
|
|
288
|
+
lastSessionSets: lastRow?._lastSessionSetsArr,
|
|
289
|
+
lastAccumulationSessionExercise: lastAccumulationRow?.lastSessionExercise ?? null,
|
|
290
|
+
lastAccumulationSessionSets: lastAccumulationRow?._lastSessionSetsArr
|
|
274
291
|
});
|
|
275
292
|
});
|
|
276
293
|
}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@aneuhold/be-ts-db-lib",
|
|
3
3
|
"author": "Anton G. Neuhold Jr.",
|
|
4
4
|
"license": "MIT",
|
|
5
|
-
"version": "4.2.
|
|
5
|
+
"version": "4.2.24",
|
|
6
6
|
"description": "A backend database library meant to actually interact with various databases in personal projects",
|
|
7
7
|
"packageManager": "pnpm@10.25.0",
|
|
8
8
|
"type": "module",
|
|
@@ -58,8 +58,8 @@
|
|
|
58
58
|
"MongoDB"
|
|
59
59
|
],
|
|
60
60
|
"dependencies": {
|
|
61
|
-
"@aneuhold/be-ts-lib": "^3.1.
|
|
62
|
-
"@aneuhold/core-ts-db-lib": "^5.0.
|
|
61
|
+
"@aneuhold/be-ts-lib": "^3.1.11",
|
|
62
|
+
"@aneuhold/core-ts-db-lib": "^5.0.6",
|
|
63
63
|
"@aneuhold/core-ts-lib": "^2.4.3",
|
|
64
64
|
"bson": "^7.0.0",
|
|
65
65
|
"google-auth-library": "^10.6.1",
|