Просмотр исходного кода

Add docModified hook in ds-mobile-app module (#27196)

* Add docModified hook in ds-mobile-app module

* use Object.entries when iterating over promises

* avoid project lookup

* update tests

GitOrigin-RevId: 88676746f56558a97ce31010b57f5eeb254fefef
Domagoj Kriskovic 1 год назад
Родитель
Сommit
d5b5710d01

+ 4 - 0
services/web/app/src/Features/Documents/DocumentController.mjs

@@ -7,6 +7,7 @@ import logger from '@overleaf/logger'
 import _ from 'lodash'
 import _ from 'lodash'
 import { plainTextResponse } from '../../infrastructure/Response.js'
 import { plainTextResponse } from '../../infrastructure/Response.js'
 import { expressify } from '@overleaf/promise-utils'
 import { expressify } from '@overleaf/promise-utils'
+import Modules from '../../infrastructure/Modules.js'
 
 
 async function getDocument(req, res) {
 async function getDocument(req, res) {
   const { Project_id: projectId, doc_id: docId } = req.params
   const { Project_id: projectId, doc_id: docId } = req.params
@@ -92,6 +93,9 @@ async function setDocument(req, res) {
     { docId, projectId },
     { docId, projectId },
     'finished receiving set document request from api (docupdater)'
     'finished receiving set document request from api (docupdater)'
   )
   )
+
+  await Modules.promises.hooks.fire('docModified', projectId, docId)
+
   res.json(result)
   res.json(result)
 }
 }
 
 

+ 1 - 2
services/web/app/src/infrastructure/Modules.js

@@ -150,8 +150,7 @@ async function linkedFileAgentsIncludes() {
 async function attachHooks() {
 async function attachHooks() {
   for (const module of await modules()) {
   for (const module of await modules()) {
     const { promises, ...hooks } = module.hooks || {}
     const { promises, ...hooks } = module.hooks || {}
-    for (const hook in promises || {}) {
-      const method = promises[hook]
+    for (const [hook, method] of Object.entries(promises || {})) {
       attachHook(hook, method)
       attachHook(hook, method)
     }
     }
     for (const hook in hooks || {}) {
     for (const hook in hooks || {}) {

+ 21 - 0
services/web/test/unit/src/Documents/DocumentController.test.mjs

@@ -87,6 +87,14 @@ describe('DocumentController', function () {
       },
       },
     }
     }
 
 
+    ctx.Modules = {
+      promises: {
+        hooks: {
+          fire: sinon.stub().resolves(),
+        },
+      },
+    }
+
     vi.doMock('../../../../app/src/Features/Project/ProjectGetter', () => ({
     vi.doMock('../../../../app/src/Features/Project/ProjectGetter', () => ({
       default: ctx.ProjectGetter,
       default: ctx.ProjectGetter,
     }))
     }))
@@ -113,6 +121,10 @@ describe('DocumentController', function () {
       default: ctx.ChatApiHandler,
       default: ctx.ChatApiHandler,
     }))
     }))
 
 
+    vi.doMock('../../../../app/src/infrastructure/Modules.js', () => ({
+      default: ctx.Modules,
+    }))
+
     ctx.DocumentController = (await import(MODULE_PATH)).default
     ctx.DocumentController = (await import(MODULE_PATH)).default
   })
   })
 
 
@@ -208,6 +220,15 @@ describe('DocumentController', function () {
       it('should return a successful response', function (ctx) {
       it('should return a successful response', function (ctx) {
         ctx.res.success.should.equal(true)
         ctx.res.success.should.equal(true)
       })
       })
+
+      it('should call the docModified hook', function (ctx) {
+        sinon.assert.calledWith(
+          ctx.Modules.promises.hooks.fire,
+          'docModified',
+          ctx.project._id,
+          ctx.doc._id
+        )
+      })
     })
     })
 
 
     describe("when the document doesn't exist", function () {
     describe("when the document doesn't exist", function () {

+ 4 - 1
services/web/types/web-module.ts

@@ -53,7 +53,10 @@ export type WebModule = {
     apply: (webRouter: any, privateApiRouter: any, publicApiRouter: any) => void
     apply: (webRouter: any, privateApiRouter: any, publicApiRouter: any) => void
   }
   }
   hooks?: {
   hooks?: {
-    [name: string]: (args: any[]) => void
+    promises?: {
+      [name: string]: (...args: any[]) => Promise<any>
+    }
+    [name: string]: ((...args: any[]) => void) | any
   }
   }
   middleware?: {
   middleware?: {
     [name: string]: RequestHandler
     [name: string]: RequestHandler