diff --git a/package-lock.json b/package-lock.json index 6522fbb9..cfaf71fa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7173,7 +7173,7 @@ }, "packages/dms": { "name": "@dvelop-sdk/dms", - "version": "1.3.2", + "version": "1.3.3", "license": "Apache-2.0", "dependencies": { "@dvelop-sdk/core": "^2.1.2" diff --git a/packages/dms/src/dms-objects/create-dms-object-notes/create-dms-object-notes.spec.ts b/packages/dms/src/dms-objects/create-dms-object-notes/create-dms-object-notes.spec.ts new file mode 100644 index 00000000..3f8ef78a --- /dev/null +++ b/packages/dms/src/dms-objects/create-dms-object-notes/create-dms-object-notes.spec.ts @@ -0,0 +1,76 @@ +import {DvelopContext} from "@dvelop-sdk/core"; +import { + _createDmsObjectNotesDefaultTransformFunction, + _createDmsObjectNotesFactory, + CreateDmsObjectNotesParams +} from "./create-dms-object-notes"; +import {HttpResponse} from "../../utils/http"; + +describe("createDmsObjectNotes", () => { + let mockHttpRequestFunction = jest.fn(); + let mockTransformFunction = jest.fn(); + + let context: DvelopContext; + let params: CreateDmsObjectNotesParams; + + beforeEach(() => { + jest.resetAllMocks(); + + context = { + systemBaseUri: "HiItsMeSystemBaseUri" + }; + + params = { + repositoryId: "HiItsMeRepositoryId", + dmsObjectId: "HiItsMeDmsObjectId", + noteText: "HiItsMeNoteText" + }; + }); + + it("should make correct request", async () => { + const createDmsObjectNotes = _createDmsObjectNotesFactory(mockHttpRequestFunction, mockTransformFunction); + await createDmsObjectNotes(context, params); + + expect(mockHttpRequestFunction).toHaveBeenCalledTimes(1); + expect(mockHttpRequestFunction).toHaveBeenCalledWith(context, { + method: "POST", + url: "/dms", + follows: ["repo", "dmsobjectwithmapping", "notes"], + templates: { + "repositoryid": params.repositoryId, + "dmsobjectid": params.dmsObjectId + }, + data: { + "text": params.noteText + } + }); + }); + + it("should pass response to transform and return transform-result", async () => { + const response: HttpResponse = { data: { test: "HiItsMeTest" } } as HttpResponse; + const transformResult: any = { result: "HiItsMeResult" }; + mockHttpRequestFunction.mockResolvedValue(response); + mockTransformFunction.mockReturnValue(transformResult); + + const createDmsObjectNotes = _createDmsObjectNotesFactory(mockHttpRequestFunction, mockTransformFunction); + await createDmsObjectNotes(context, params); + + expect(mockTransformFunction).toHaveBeenCalledTimes(1); + expect(mockTransformFunction).toHaveBeenCalledWith(response, context, params); + }); + + describe("createDmsObjectNotesDefaultTransformFunction", function () { + it("should map with values for all properties", async() => { + const response: HttpResponse = { data: { test: "HiItsMeTest" } } as HttpResponse; + mockHttpRequestFunction.mockResolvedValue(response); + mockTransformFunction.mockReturnValue(params); + + const createDmsObjectNotes = _createDmsObjectNotesFactory(mockHttpRequestFunction, _createDmsObjectNotesDefaultTransformFunction); + const result: CreateDmsObjectNotesParams = await createDmsObjectNotes(context, params); + + expect(result).toHaveProperty("repositoryId", params.repositoryId); + expect(result).toHaveProperty("dmsObjectId", params.dmsObjectId); + expect(result).toHaveProperty("noteText", params.noteText); + }); + }); +}); diff --git a/packages/dms/src/dms-objects/create-dms-object-notes/create-dms-object-notes.ts b/packages/dms/src/dms-objects/create-dms-object-notes/create-dms-object-notes.ts new file mode 100644 index 00000000..13c211f2 --- /dev/null +++ b/packages/dms/src/dms-objects/create-dms-object-notes/create-dms-object-notes.ts @@ -0,0 +1,80 @@ +import {_defaultHttpRequestFunction, HttpConfig, HttpResponse} from "../../utils/http"; +import {DvelopContext} from "../../index"; + +/** + * Parameters for the {@link createDmsObjectNotes}-function. + * @category DmsObject + */ +export interface CreateDmsObjectNotesParams { + /** ID of the repository */ + repositoryId: string; + + /** ID of the DmsObject */ + dmsObjectId: string; + + /** Text for the note */ + noteText: string; +} + +/** + * Default transform-function provided to the {@link createDmsObjectNotes}-function. See [Advanced Topics](https://github.com/d-velop/dvelop-sdk-node#advanced-topics) for more information. + * @internal + * @category DmsObject + */ +export function _createDmsObjectNotesDefaultTransformFunction(response: HttpResponse, _: DvelopContext, params: CreateDmsObjectNotesParams): CreateDmsObjectNotesParams { + return { + repositoryId: params.repositoryId, + dmsObjectId: params.dmsObjectId, + noteText: params.noteText + }; +} + +/** + * Factory for the {@link createDmsObjectNotes}-function. See [Advanced Topics](https://github.com/d-velop/dvelop-sdk-node#advanced-topics) for more information. + * @typeparam T Return type of the {@link createDmsObjectNotes}-function. A corresponding transformFunction has to be supplied. + * @category DmsObject + */ +export function _createDmsObjectNotesFactory( + httpRequestFunction: (context: DvelopContext, config: HttpConfig) => Promise, + transformFunction: (response: HttpResponse, context: DvelopContext, params: CreateDmsObjectNotesParams) => T +): (context: DvelopContext, params: CreateDmsObjectNotesParams) => Promise { + return async (context: DvelopContext, params: CreateDmsObjectNotesParams) => { + const response: HttpResponse = await httpRequestFunction(context, { + method: "POST", + url: "/dms", + follows: ["repo", "dmsobjectwithmapping", "notes"], + templates: { + "repositoryid": params.repositoryId, + "dmsobjectid": params.dmsObjectId + }, + data: { + "text": params.noteText + } + }); + + return transformFunction(response, context, params); + }; +} + +/** + * Create a note for an existing DmsObject. + * + * ```typescript + * import { createDmsObjectNotes } from "@dvelop-sdk/dms"; + * + * await createDmsObjectNotes({ + * systemBaseUri: "https://steamwheedle-cartel.d-velop.cloud", + * authSessionId: "dQw4w9WgXcQ" + * }, { + * repositoryId: "qnydFmqHuVo", + * dmsObjectId: "GDYQ3PJKrT8", + * noteText: "Test note" + * }); + * ``` + * + * @category DmsObject + */ +/* istanbul ignore next */ +export async function createDmsObjectNotes(context: DvelopContext, params: CreateDmsObjectNotesParams): Promise { + return await _createDmsObjectNotesFactory(_defaultHttpRequestFunction, _createDmsObjectNotesDefaultTransformFunction)(context, params); +} diff --git a/packages/dms/src/index.ts b/packages/dms/src/index.ts index dc5a1114..637bbfe7 100644 --- a/packages/dms/src/index.ts +++ b/packages/dms/src/index.ts @@ -37,6 +37,7 @@ export { getRepositories } from "./repositories/get-repositories/get-repositorie export { GetDmsObjectParams, DmsObject, getDmsObject } from "./dms-objects/get-dms-object/get-dms-object"; export { getDmsObjectMainFile, getDmsObjectPdfFile } from "./dms-objects/get-dms-object-file/get-dms-object-file"; export { CreateDmsObjectParams, createDmsObject } from "./dms-objects/create-dms-object/create-dms-object"; +export { CreateDmsObjectNotesParams, createDmsObjectNotes } from "./dms-objects/create-dms-object-notes/create-dms-object-notes"; export { StoreFileTemporarilyParams, storeFileTemporarily } from "./dms-objects/store-file-temporarily/store-file-temporarily"; export { UpdateDmsObjectParams, updateDmsObject } from "./dms-objects/update-dms-object/update-dms-object"; export { UpdateDmsObjectStatusParams, updateDmsObjectStatus } from "./dms-objects/update-dms-object-status/update-dms-object-status";