-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreleasesFactory.ts
More file actions
52 lines (45 loc) · 1.38 KB
/
releasesFactory.ts
File metadata and controls
52 lines (45 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import type { Collection, Db, ObjectId } from 'mongodb';
import type { ReleaseDBScheme } from '@hawk.so/types';
/**
* ReleasesFactory
* Helper for accessing releases collection
*/
export default class ReleasesFactory {
/**
* DataBase collection to work with
*/
private readonly collection: Collection<ReleaseDBScheme>;
/**
* Creates releases factory instance
* @param dbConnection - connection to Events DB
*/
constructor(dbConnection: Db) {
this.collection = dbConnection.collection<ReleaseDBScheme>('releases');
}
/**
* Find one release document by projectId and release label.
* Tries both exact string match and numeric fallback (if release can be cast to number).
*/
public async findByProjectAndRelease(
projectId: string | ObjectId,
release: string
): Promise<ReleaseDBScheme | null> {
const projectIdStr = projectId.toString();
// Try exact match as stored
let doc = await this.collection.findOne({
projectId: projectIdStr,
release: release as ReleaseDBScheme['release'],
});
// Fallback if release stored as number
if (!doc) {
const asNumber = Number(release);
if (!Number.isNaN(asNumber)) {
doc = await this.collection.findOne({
projectId: projectIdStr,
release: asNumber as unknown as ReleaseDBScheme['release'],
});
}
}
return doc;
}
}