Merge pull request #2951 from specklesystems/fabians/fileuploads-ioc-3
chore(server): fileuploads IoC 3 - insertNewUploadAndNotifyFactory
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import { FileUploadRecord } from '@/modules/fileuploads/helpers/types'
|
||||
import { SaveUploadFileInput } from '@/modules/fileuploads/repositories/fileUploads'
|
||||
import { Optional } from '@speckle/shared'
|
||||
|
||||
export type GetFileInfo = (args: {
|
||||
fileId: string
|
||||
}) => Promise<Optional<FileUploadRecord>>
|
||||
|
||||
export type SaveUploadFile = (args: SaveUploadFileInput) => Promise<FileUploadRecord>
|
||||
|
||||
+38
-20
@@ -1,20 +1,40 @@
|
||||
/* istanbul ignore file */
|
||||
const {
|
||||
insertNewUploadAndNotify
|
||||
} = require('@/modules/fileuploads/services/management')
|
||||
const request = require('request')
|
||||
const { streamWritePermissions } = require('@/modules/shared/authz')
|
||||
const { authMiddlewareCreator } = require('@/modules/shared/middleware')
|
||||
const { moduleLogger } = require('@/logging/logging')
|
||||
const {
|
||||
listenForImportUpdatesFactory
|
||||
} = require('@/modules/fileuploads/services/resultListener')
|
||||
const { getFileInfoFactory } = require('@/modules/fileuploads/repositories/fileUploads')
|
||||
const { db } = require('@/db/knex')
|
||||
const { publish } = require('@/modules/shared/utils/subscriptions')
|
||||
const { getStreamBranchByName } = require('@/modules/core/repositories/branches')
|
||||
import { insertNewUploadAndNotifyFactory } from '@/modules/fileuploads/services/management'
|
||||
import request from 'request'
|
||||
import { streamWritePermissions } from '@/modules/shared/authz'
|
||||
import { authMiddlewareCreator } from '@/modules/shared/middleware'
|
||||
import { moduleLogger } from '@/logging/logging'
|
||||
import { listenForImportUpdatesFactory } from '@/modules/fileuploads/services/resultListener'
|
||||
import {
|
||||
getFileInfoFactory,
|
||||
saveUploadFileFactory
|
||||
} from '@/modules/fileuploads/repositories/fileUploads'
|
||||
import { db } from '@/db/knex'
|
||||
import { publish } from '@/modules/shared/utils/subscriptions'
|
||||
import { getStreamBranchByName } from '@/modules/core/repositories/branches'
|
||||
import { SpeckleModule } from '@/modules/shared/helpers/typeHelper'
|
||||
|
||||
const saveFileUploads = async ({ userId, streamId, branchName, uploadResults }) => {
|
||||
const insertNewUploadAndNotify = insertNewUploadAndNotifyFactory({
|
||||
getStreamBranchByName,
|
||||
saveUploadFile: saveUploadFileFactory({ db }),
|
||||
publish
|
||||
})
|
||||
|
||||
const saveFileUploads = async ({
|
||||
userId,
|
||||
streamId,
|
||||
branchName,
|
||||
uploadResults
|
||||
}: {
|
||||
userId: string
|
||||
streamId: string
|
||||
branchName: string
|
||||
uploadResults: Array<{
|
||||
blobId: string
|
||||
fileName: string
|
||||
fileSize: number
|
||||
}>
|
||||
}) => {
|
||||
await Promise.all(
|
||||
uploadResults.map(async (upload) => {
|
||||
await insertNewUploadAndNotify({
|
||||
@@ -23,14 +43,14 @@ const saveFileUploads = async ({ userId, streamId, branchName, uploadResults })
|
||||
branchName,
|
||||
userId,
|
||||
fileName: upload.fileName,
|
||||
fileType: upload.fileName.split('.').pop(),
|
||||
fileType: upload.fileName.split('.').pop()!,
|
||||
fileSize: upload.fileSize
|
||||
})
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
exports.init = async (app, isInitial) => {
|
||||
export const init: SpeckleModule['init'] = async (app, isInitial) => {
|
||||
if (process.env.DISABLE_FILE_UPLOADS) {
|
||||
moduleLogger.warn('📄 FileUploads module is DISABLED')
|
||||
return
|
||||
@@ -60,7 +80,7 @@ exports.init = async (app, isInitial) => {
|
||||
if (response.statusCode === 201) {
|
||||
const { uploadResults } = JSON.parse(body)
|
||||
await saveFileUploads({
|
||||
userId: req.context.userId,
|
||||
userId: req.context.userId!,
|
||||
streamId: req.params.streamId,
|
||||
branchName,
|
||||
uploadResults
|
||||
@@ -91,5 +111,3 @@ exports.init = async (app, isInitial) => {
|
||||
listenForImportUpdates()
|
||||
}
|
||||
}
|
||||
|
||||
exports.finalize = () => {}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Branches, FileUploads, knex } from '@/modules/core/dbSchema'
|
||||
import { GetFileInfo } from '@/modules/fileuploads/domain/operations'
|
||||
import { GetFileInfo, SaveUploadFile } from '@/modules/fileuploads/domain/operations'
|
||||
import {
|
||||
FileUploadConvertedStatus,
|
||||
FileUploadRecord
|
||||
@@ -50,28 +50,30 @@ export type SaveUploadFileInput = Pick<
|
||||
'streamId' | 'branchName' | 'userId' | 'fileName' | 'fileType' | 'fileSize'
|
||||
> & { fileId: string }
|
||||
|
||||
export async function saveUploadFile({
|
||||
fileId,
|
||||
streamId,
|
||||
branchName,
|
||||
userId,
|
||||
fileName,
|
||||
fileType,
|
||||
fileSize
|
||||
}: SaveUploadFileInput) {
|
||||
const dbFile: Partial<FileUploadRecord> = {
|
||||
id: fileId,
|
||||
export const saveUploadFileFactory =
|
||||
(deps: { db: Knex }): SaveUploadFile =>
|
||||
async ({
|
||||
fileId,
|
||||
streamId,
|
||||
branchName,
|
||||
userId,
|
||||
fileName,
|
||||
fileType,
|
||||
fileSize,
|
||||
uploadComplete: true
|
||||
fileSize
|
||||
}: SaveUploadFileInput) => {
|
||||
const dbFile: Partial<FileUploadRecord> = {
|
||||
id: fileId,
|
||||
streamId,
|
||||
branchName,
|
||||
userId,
|
||||
fileName,
|
||||
fileType,
|
||||
fileSize,
|
||||
uploadComplete: true
|
||||
}
|
||||
const [newRecord] = await tables.fileUploads(deps.db).insert(dbFile, '*')
|
||||
return newRecord as FileUploadRecord
|
||||
}
|
||||
const [newRecord] = await FileUploads.knex().insert(dbFile, '*')
|
||||
return newRecord as FileUploadRecord
|
||||
}
|
||||
|
||||
const getPendingUploadsBaseQuery = (
|
||||
streamId: string,
|
||||
|
||||
@@ -4,43 +4,50 @@ import {
|
||||
ProjectPendingVersionsUpdatedMessageType
|
||||
} from '@/modules/core/graph/generated/graphql'
|
||||
import { getStreamBranchByName } from '@/modules/core/repositories/branches'
|
||||
import { SaveUploadFile } from '@/modules/fileuploads/domain/operations'
|
||||
import { SaveUploadFileInput } from '@/modules/fileuploads/repositories/fileUploads'
|
||||
import {
|
||||
saveUploadFile,
|
||||
SaveUploadFileInput
|
||||
} from '@/modules/fileuploads/repositories/fileUploads'
|
||||
import { FileImportSubscriptions, publish } from '@/modules/shared/utils/subscriptions'
|
||||
FileImportSubscriptions,
|
||||
PublishSubscription
|
||||
} from '@/modules/shared/utils/subscriptions'
|
||||
|
||||
export async function insertNewUploadAndNotify(upload: SaveUploadFileInput) {
|
||||
const branch = await getStreamBranchByName(upload.streamId, upload.branchName)
|
||||
const file = await saveUploadFile(upload)
|
||||
export const insertNewUploadAndNotifyFactory =
|
||||
(deps: {
|
||||
getStreamBranchByName: typeof getStreamBranchByName
|
||||
saveUploadFile: SaveUploadFile
|
||||
publish: PublishSubscription
|
||||
}) =>
|
||||
async (upload: SaveUploadFileInput) => {
|
||||
const branch = await deps.getStreamBranchByName(upload.streamId, upload.branchName)
|
||||
const file = await deps.saveUploadFile(upload)
|
||||
|
||||
if (!branch) {
|
||||
await publish(FileImportSubscriptions.ProjectPendingModelsUpdated, {
|
||||
projectPendingModelsUpdated: {
|
||||
if (!branch) {
|
||||
await deps.publish(FileImportSubscriptions.ProjectPendingModelsUpdated, {
|
||||
projectPendingModelsUpdated: {
|
||||
id: file.id,
|
||||
type: ProjectPendingModelsUpdatedMessageType.Created,
|
||||
model: file
|
||||
},
|
||||
projectId: file.streamId
|
||||
})
|
||||
} else {
|
||||
await deps.publish(FileImportSubscriptions.ProjectPendingVersionsUpdated, {
|
||||
projectPendingVersionsUpdated: {
|
||||
id: file.id,
|
||||
type: ProjectPendingVersionsUpdatedMessageType.Created,
|
||||
version: file
|
||||
},
|
||||
projectId: file.streamId,
|
||||
branchName: file.branchName
|
||||
})
|
||||
}
|
||||
|
||||
await deps.publish(FileImportSubscriptions.ProjectFileImportUpdated, {
|
||||
projectFileImportUpdated: {
|
||||
id: file.id,
|
||||
type: ProjectPendingModelsUpdatedMessageType.Created,
|
||||
model: file
|
||||
type: ProjectFileImportUpdatedMessageType.Created,
|
||||
upload: file
|
||||
},
|
||||
projectId: file.streamId
|
||||
})
|
||||
} else {
|
||||
await publish(FileImportSubscriptions.ProjectPendingVersionsUpdated, {
|
||||
projectPendingVersionsUpdated: {
|
||||
id: file.id,
|
||||
type: ProjectPendingVersionsUpdatedMessageType.Created,
|
||||
version: file
|
||||
},
|
||||
projectId: file.streamId,
|
||||
branchName: file.branchName
|
||||
})
|
||||
}
|
||||
|
||||
await publish(FileImportSubscriptions.ProjectFileImportUpdated, {
|
||||
projectFileImportUpdated: {
|
||||
id: file.id,
|
||||
type: ProjectFileImportUpdatedMessageType.Created,
|
||||
upload: file
|
||||
},
|
||||
projectId: file.streamId
|
||||
})
|
||||
}
|
||||
|
||||
@@ -153,6 +153,7 @@
|
||||
"@types/passport-google-oauth20": "^2.0.16",
|
||||
"@types/pg": "^8.6.6",
|
||||
"@types/pino-http": "^5.8.4",
|
||||
"@types/request": "^2.48.12",
|
||||
"@types/sanitize-html": "^2.6.2",
|
||||
"@types/supertest": "^2.0.12",
|
||||
"@types/ua-parser-js": "^0.7.39",
|
||||
|
||||
@@ -15450,6 +15450,7 @@ __metadata:
|
||||
"@types/passport-google-oauth20": "npm:^2.0.16"
|
||||
"@types/pg": "npm:^8.6.6"
|
||||
"@types/pino-http": "npm:^5.8.4"
|
||||
"@types/request": "npm:^2.48.12"
|
||||
"@types/sanitize-html": "npm:^2.6.2"
|
||||
"@types/supertest": "npm:^2.0.12"
|
||||
"@types/ua-parser-js": "npm:^0.7.39"
|
||||
@@ -19000,7 +19001,7 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@types/request@npm:^2.48.8":
|
||||
"@types/request@npm:^2.48.12, @types/request@npm:^2.48.8":
|
||||
version: 2.48.12
|
||||
resolution: "@types/request@npm:2.48.12"
|
||||
dependencies:
|
||||
|
||||
Reference in New Issue
Block a user