Skip to content

Commit

Permalink
Merge pull request #174 from d-velop/develop
Browse files Browse the repository at this point in the history
Add getDmsObjectNotes-function
  • Loading branch information
LenKlose authored Oct 13, 2023
2 parents 41de7bf + 97b4e6c commit 2f43996
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/dms/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@dvelop-sdk/dms",
"description": "This package contains functionality for the DMS-App in the d.velop cloud.",
"version": "1.4.0",
"version": "1.5.1",
"license": "Apache-2.0",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand All @@ -25,4 +25,4 @@
"dependencies": {
"@dvelop-sdk/core": "^2.1.2"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { DvelopContext } from "@dvelop-sdk/core";
import {
_getDmsObjectNotesDefaultTransformFunction,
_getDmsObjectNotesFactory,
GetDmsObjectNotesParams,
DmsObjectNote
} from "./get-dms-object-notes";
import { HttpResponse } from "../../utils/http";

describe("getDmsObjectNotes", () => {
let mockHttpRequestFunction = jest.fn();
let mockTransformFunction = jest.fn();

let context: DvelopContext;
let params: GetDmsObjectNotesParams;

beforeEach(() => {
jest.resetAllMocks();

context = {
systemBaseUri: "HiItsMeSystemBaseUri"
};

params = {
repositoryId: "HiItsMeRepositoryId",
dmsObjectId: "HiItsMeDmsObjectId"
};
});

it("should make correct request", async () => {
const getDmsObjectNotes = _getDmsObjectNotesFactory(mockHttpRequestFunction, mockTransformFunction);
await getDmsObjectNotes(context, params);

expect(mockHttpRequestFunction).toHaveBeenCalledTimes(1);
expect(mockHttpRequestFunction).toHaveBeenCalledWith(context, {
method: "GET",
url: "/dms",
follows: ["repo", "dmsobjectwithmapping", "notes"],
templates: {
"repositoryid": params.repositoryId,
"dmsobjectid": params.dmsObjectId
}
});
});

it("should pass response to transform and return transform-result", async () => {
const response: HttpResponse = {
data: {
notes: [{
creator: {
id: "HiItsMeCreatorId",
displayName: "HiItsMeCreatorDisplayName"
},
text: "HiItsMeText",
created: "2023-10-11T09:09:09.453+02:00"
}]
}
} as HttpResponse;

const transformResult: DmsObjectNote[] = [{
creator: {
id: "HiItsMeCreatorId",
displayName: "HiItsMeCreatorDisplayName"
},
text: "HiItsMeText",
created: new Date("2023-10-11T09:09:09.453+02:00")
}];

mockHttpRequestFunction.mockResolvedValue(response);
mockTransformFunction.mockReturnValue(transformResult);

const getDmsObjectNotes = _getDmsObjectNotesFactory(mockHttpRequestFunction, mockTransformFunction);
await getDmsObjectNotes(context, params);

expect(mockTransformFunction).toHaveBeenCalledTimes(1);
expect(mockTransformFunction).toHaveBeenCalledWith(response, context, params);
});

describe("getDmsObjectNotesDefaultTransformFunction", () => {
it("should return response DmsObjectNotes with single note", async () => {
const response: HttpResponse = {
data: {
notes: [{
creator: {
id: "HiItsMeCreatorId",
displayName: "HiItsMeCreatorDisplayName"
},
text: "HiItsMeText",
created: "2023-10-11T09:09:09.453+02:00"
}]
}
} as HttpResponse;

const expectedResult: DmsObjectNote[] = [{
creator: {
id: "HiItsMeCreatorId",
displayName: "HiItsMeCreatorDisplayName"
},
text: "HiItsMeText",
created: new Date("2023-10-11T09:09:09.453+02:00")
}];

mockHttpRequestFunction.mockResolvedValue(response);
const getDmsObjectNotes = _getDmsObjectNotesFactory(mockHttpRequestFunction, _getDmsObjectNotesDefaultTransformFunction);
const result = await getDmsObjectNotes(context, params);

expect(result).toEqual(expectedResult);
});

it("should return response DmsObjectNotes without notes when dmsObject has no notes", async () => {
const response: HttpResponse = {
data: {
notes: []
}
} as HttpResponse;

const expectedResult: any = [];

mockHttpRequestFunction.mockResolvedValue(response);
const getDmsObjectNotes = _getDmsObjectNotesFactory(mockHttpRequestFunction, _getDmsObjectNotesDefaultTransformFunction);
const result = await getDmsObjectNotes(context, params);

expect(result).toEqual(expectedResult);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { _defaultHttpRequestFunction, HttpConfig, HttpResponse } from "../../utils/http";
import { DvelopContext } from "../../index";

/**
* Parameters for the {@link getDmsObjectNotes}-function.
* @category DmsObject
*/
export interface GetDmsObjectNotesParams {
/** ID of the repository */
repositoryId: string;
/** ID of the DmsObject */
dmsObjectId: string;
}

/**
* All information provided for a single note for the {@link DmsObjectNotes}-interface.
* @category DmsObject
*/
export interface DmsObjectNote {
/* Creator of the DmsObjectNotes */
creator: {
/* ID of the creator of the note */
id: string;
/* DisplayName is the full name of the creator */
displayName: string;
},
/* Text of the note */
text: string;
/* Creation date of the note */
created: Date;
}

/**
* Default transform-function provided to the {@link getDmsObjectNotes}-function. See [Advanced Topics](https://github.com/d-velop/dvelop-sdk-node#advanced-topics) for more information.
* @internal
* @category DmsObject
*/
export function _getDmsObjectNotesDefaultTransformFunction(response: HttpResponse<any>, _: DvelopContext, __: GetDmsObjectNotesParams): DmsObjectNote[] {
const mappedNotes: DmsObjectNote[] = response.data.notes.map((note: DmsObjectNote) => {
return {
creator: {
id: note.creator.id,
displayName: note.creator.displayName
},
text: note.text,
created: new Date(note.created)
};
});

return mappedNotes;
}

/**
* Factory for the {@link getDmsObjectNotes}-function. See [Advanced Topics](https://github.com/d-velop/dvelop-sdk-node#advanced-topics) for more information.
* @typeparam T Return type of the {@link getDmsObjectNotes}-function. A corresponding transformFunction has to be supplied.
* @category DmsObject
*/
export function _getDmsObjectNotesFactory<T>(
httpRequestFunction: (context: DvelopContext, config: HttpConfig) => Promise<HttpResponse>,
transformFunction: (response: HttpResponse, context: DvelopContext, params: GetDmsObjectNotesParams) => T
): (context: DvelopContext, params: GetDmsObjectNotesParams) => Promise<T> {
return async (context: DvelopContext, params: GetDmsObjectNotesParams) => {
const response: HttpResponse = await httpRequestFunction(context, {
method: "GET",
url: "/dms",
follows: ["repo", "dmsobjectwithmapping", "notes"],
templates: {
"repositoryid": params.repositoryId,
"dmsobjectid": params.dmsObjectId
}
});

return transformFunction(response, context, params);
};
}

/**
* Get all notes for an existing DmsObject.
*
* ```typescript
* import { getDmsObjectNotes } from "@dvelop-sdk/dms";
*
* const notes: DmsObjectNote[] = getDmsObjectNotes({
* systemBaseUri: "https://steamwheedle-cartel.d-velop.cloud",
* authSessionId: "dQw4w9WgXcQ"
* }, {
* repositoryId: "qnydFmqHuVo",
* dmsObjectId: "GDYQ3PJKrT8"
* });
*
* notes.forEach(n => {
* console.log(`${n.creator.displayName}: "${n.text}"`);
* });
*
* // Jastor Gallywix: "I need this taken care of ASAP!"
* // Bing Zapcrackle: "I'm on it my prince."
* ```
*
* @category DmsObject
*/
/* istanbul ignore next */
export async function getDmsObjectNotes(context: DvelopContext, params: GetDmsObjectNotesParams): Promise<DmsObjectNote[]> {
return await _getDmsObjectNotesFactory(_defaultHttpRequestFunction, _getDmsObjectNotesDefaultTransformFunction)(context, params);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { DvelopContext, getDmsObjectMainFile, getDmsObjectPdfFile, searchDmsObjects, SearchDmsObjectsResultPage } from "../../index";
import { DmsObjectNote, DvelopContext, getDmsObjectMainFile, getDmsObjectNotes, getDmsObjectPdfFile, searchDmsObjects, SearchDmsObjectsResultPage } from "../../index";
import { HttpResponse } from "../../utils/http";
import { GetDmsObjectParams, _getDmsObjectFactory, _getDmsObjectDefaultTransformFunction, DmsObject } from "../get-dms-object/get-dms-object";

jest.mock("../get-dms-object-file/get-dms-object-file");
jest.mock("../search-dms-objects/search-dms-objects");
jest.mock("../get-dms-object-notes/get-dms-object-notes");
const mockGetDmsObjectMainFile = getDmsObjectMainFile as jest.MockedFunction<typeof getDmsObjectMainFile>;
const mockGetDmsObjectPdfFile = getDmsObjectPdfFile as jest.MockedFunction<typeof getDmsObjectPdfFile>;
const mockSearchDmsObjects = searchDmsObjects as jest.MockedFunction<typeof searchDmsObjects>;
const mockGetDmsObjectNotes = getDmsObjectNotes as jest.MockedFunction<typeof getDmsObjectNotes>;

describe("getDmsObject", () => {

Expand Down Expand Up @@ -131,6 +133,7 @@ describe("getDmsObject", () => {

expect(result.getMainFile).toEqual(expect.any(Function));

// @ts-ignore: Object is possibly 'null'.
const resultFile: ArrayBuffer = await result.getMainFile();
expect(mockGetDmsObjectMainFile).toHaveBeenCalledTimes(1);
expect(mockGetDmsObjectMainFile).toHaveBeenCalledWith(context, params);
Expand Down Expand Up @@ -160,6 +163,7 @@ describe("getDmsObject", () => {

expect(result.getPdfFile).toEqual(expect.any(Function));

// @ts-ignore: Object is possibly 'null'.
const resultFile: ArrayBuffer = await result.getPdfFile();
expect(mockGetDmsObjectPdfFile).toHaveBeenCalledTimes(1);
expect(mockGetDmsObjectPdfFile).toHaveBeenCalledWith(context, params);
Expand Down Expand Up @@ -192,6 +196,8 @@ describe("getDmsObject", () => {

expect(result.searchChildren).toEqual(expect.any(Function));


// @ts-ignore: Object is possibly 'null'.
const resultChildren: SearchDmsObjectsResultPage = await result.searchChildren();
expect(mockSearchDmsObjects).toHaveBeenCalledTimes(1);
expect(mockSearchDmsObjects).toHaveBeenCalledWith(context, {
Expand All @@ -201,5 +207,44 @@ describe("getDmsObject", () => {
});
expect(resultChildren).toBe(searchResultPage);
});

it("should set getNotes correctly", async () => {

const data: any = {
"_links": {
"notes": {
"href": "HiItsMeNotedHref"
},
},
"id": "HiItsMeId",
"sourceProperties": [],
"sourceCategories": []
};

const response: HttpResponse = { data: data } as HttpResponse;
mockHttpRequestFunction.mockResolvedValue(response);

const notes: DmsObjectNote[] = [{
created: new Date(),
creator: {
id: "HiItsMeCreatorId",
displayName: "HiItsMeCreatorDisplayName"
},
text: "HiItsMeNoteText"
}];
mockGetDmsObjectNotes.mockResolvedValue(notes);

const getDmsObject = _getDmsObjectFactory(mockHttpRequestFunction, _getDmsObjectDefaultTransformFunction);
const result: DmsObject = await getDmsObject(context, params);

expect(result.getNotes).toEqual(expect.any(Function));


// @ts-ignore: Object is possibly 'null'.
const resultNotes: DmsObjectNote[] = await result.getNotes();
expect(mockGetDmsObjectNotes).toHaveBeenCalledTimes(1);
expect(mockGetDmsObjectNotes).toHaveBeenCalledWith(context, params);
expect(resultNotes).toBe(notes);
});
});
});
10 changes: 8 additions & 2 deletions packages/dms/src/dms-objects/get-dms-object/get-dms-object.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DvelopContext, SearchDmsObjectsResultPage, getDmsObjectMainFile, getDmsObjectPdfFile, SearchDmsObjectsParams, searchDmsObjects } from "../../index";
import { DvelopContext, SearchDmsObjectsResultPage, getDmsObjectMainFile, getDmsObjectPdfFile, SearchDmsObjectsParams, searchDmsObjects, DmsObjectNote, getDmsObjectNotes } from "../../index";
import { HttpConfig, HttpResponse, _defaultHttpRequestFunction } from "../../utils/http";

/**
Expand Down Expand Up @@ -44,8 +44,10 @@ export interface DmsObject {
getMainFile?: () => Promise<ArrayBuffer>;
/** Function that returns the DmsObject-pdf. */
getPdfFile?: () => Promise<ArrayBuffer>;

/** Function that returns a searchresult of all children. */
searchChildren?: () => Promise<SearchDmsObjectsResultPage>;
/** Function that returns the notes of a DmsObject. */
getNotes?: () => Promise<DmsObjectNote[]>;
}

/**
Expand Down Expand Up @@ -84,6 +86,10 @@ export function _getDmsObjectDefaultTransformFunctionFactory(
}));
}

if (response.data._links.notes) {
dmsObject.getNotes = async () => (await getDmsObjectNotes(context, params));
}

return dmsObject;
};
}
Expand Down
1 change: 1 addition & 0 deletions packages/dms/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export { getRepositories } from "./repositories/get-repositories/get-repositorie
// DmsObjects
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 { GetDmsObjectNotesParams, DmsObjectNote, getDmsObjectNotes } from "./dms-objects/get-dms-object-notes/get-dms-object-notes";
export { CreateDmsObjectParams, createDmsObject } from "./dms-objects/create-dms-object/create-dms-object";
export { CreateDmsObjectNoteParams, createDmsObjectNote as createDmsObjectNotes } from "./dms-objects/create-dms-object-note/create-dms-object-note";
export { StoreFileTemporarilyParams, storeFileTemporarily } from "./dms-objects/store-file-temporarily/store-file-temporarily";
Expand Down
3 changes: 2 additions & 1 deletion packages/dms/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ export { _getRepositoriesFactory, _getRepositoriesDefaultTransformFunction } fro
// DmsObjects
export { _getDmsObjectFactory, _getDmsObjectDefaultTransformFunction, _getDmsObjectDefaultTransformFunctionFactory } from "./dms-objects/get-dms-object/get-dms-object";
export { _getDmsObjectMainFileFactory, _getDmsObjectPdfFileFactory } from "./dms-objects/get-dms-object-file/get-dms-object-file";
export { _getDmsObjectNotesFactory, _getDmsObjectNotesDefaultTransformFunction } from "./dms-objects/get-dms-object-notes/get-dms-object-notes";
export { _createDmsObjectDefaultStoreFileFunction, _createDmsObjectDefaultTransformFunction } from "./dms-objects/create-dms-object/create-dms-object";
export { _createDmsObjectNoteFactory, _createDmsObjectNoteDefaultTransformFunction } from "./dms-objects/create-dms-object-note/create-dms-object-note"
export { _createDmsObjectNoteFactory, _createDmsObjectNoteDefaultTransformFunction } from "./dms-objects/create-dms-object-note/create-dms-object-note";
export { _storeFileTemporarilyFactory, _storeFileTemporarilyDefaultTransformFunction } from "./dms-objects/store-file-temporarily/store-file-temporarily";
export { _updateDmsObjectFactory, _updateDmsObjectDefaultTransformFunction } from "./dms-objects/update-dms-object/update-dms-object";
export { _updateDmsObjectStatusFactory, _updateDmsObjectStatusDefaultTransformFunction } from "./dms-objects/update-dms-object-status/update-dms-object-status";
Expand Down

0 comments on commit 2f43996

Please sign in to comment.