Skip to content

Commit d50a89e

Browse files
committed
fix: move find files to the factory
1 parent 571be66 commit d50a89e

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

src/models/releasesFactory.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,42 @@
11
import type { Collection, Db, ObjectId } from 'mongodb';
22
import type { ReleaseDBScheme } from '@hawk.so/types';
33

4+
/**
5+
* Interface representing how release files are stored in the DB
6+
*/
7+
export interface ReleaseFileDBScheme {
8+
/**
9+
* File's id
10+
*/
11+
_id: ObjectId;
12+
13+
/**
14+
* File length in bytes
15+
*/
16+
length: number;
17+
18+
/**
19+
* File upload date
20+
*/
21+
uploadDate: Date;
22+
23+
/**
24+
* File chunk size
25+
*/
26+
chunkSize: number;
27+
28+
/**
29+
* File map name
30+
*/
31+
filename: string;
32+
33+
/**
34+
* File MD5 hash
35+
*/
36+
md5: string;
37+
}
38+
39+
440
/**
541
* ReleasesFactory
642
* Helper for accessing releases collection
@@ -10,13 +46,15 @@ export default class ReleasesFactory {
1046
* DataBase collection to work with
1147
*/
1248
private readonly collection: Collection<ReleaseDBScheme>;
49+
private readonly filesCollection: Collection<ReleaseFileDBScheme>;
1350

1451
/**
1552
* Creates releases factory instance
1653
* @param dbConnection - connection to Events DB
1754
*/
1855
constructor(dbConnection: Db) {
1956
this.collection = dbConnection.collection<ReleaseDBScheme>('releases');
57+
this.filesCollection = dbConnection.collection<ReleaseFileDBScheme>('releases.files');
2058
}
2159

2260
/**
@@ -49,4 +87,13 @@ export default class ReleasesFactory {
4987

5088
return doc;
5189
}
90+
91+
/**
92+
* Find files by release id
93+
* @param releaseId - release id
94+
* @returns files
95+
*/
96+
public async findFilesByReleaseId(releaseId: string): Promise<ReleaseFileDBScheme[]> {
97+
return this.filesCollection.find({ releaseId }).toArray();
98+
}
5299
}

src/resolvers/project.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -579,24 +579,16 @@ module.exports = {
579579
// If there are files to enrich, try to get their metadata
580580
if (enrichedFiles.length > 0) {
581581
try {
582-
const filesColl = mongo.databases.events.collection('releases.files');
583-
584582
const fileIds = [ ...new Set(
585583
enrichedFiles
586584
.filter(file => file && typeof file === 'object' && file._id)
587585
.map(file => String(file._id))
588586
) ].map(id => new ObjectId(id));
589587

590588
if (fileIds.length > 0) {
591-
const filesInfo = await filesColl.find(
592-
{ _id: { $in: fileIds } },
593-
{
594-
projection: {
595-
length: 1,
596-
uploadDate: 1,
597-
},
598-
}
599-
).toArray();
589+
const filesInfo = await factories.releasesFactory.findFilesByReleaseId(
590+
releaseDoc._id.toString()
591+
);
600592

601593
const metaById = new Map(
602594
filesInfo.map(fileInfo => [String(fileInfo._id), {

0 commit comments

Comments
 (0)