Skip to content

Commit

Permalink
Merge pull request #193 from patptrs/hotfix/fix-complete-task
Browse files Browse the repository at this point in the history
🐛 Fix completeTask function in task package
  • Loading branch information
LenKlose authored Apr 10, 2024
2 parents 92d2ba0 + bdea7eb commit 292df49
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
31 changes: 27 additions & 4 deletions packages/task/src/tasks/complete-task/complete-task.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DvelopContext } from "@dvelop-sdk/core";
import { HttpResponse } from "../../utils/http";
import { HttpResponse, TaskError } from "../../utils/http";
import { CompleteTaskParams, _completeTaskFactory } from "./complete-task";

describe("completeTaskFactory", () => {
Expand All @@ -19,7 +19,7 @@ describe("completeTaskFactory", () => {
};

params = {
location: "HiItsMeLocation"
location: "/task/tasks/HiItsMeLocation"
};
});

Expand All @@ -31,8 +31,7 @@ describe("completeTaskFactory", () => {
expect(mockHttpRequestFunction).toHaveBeenCalledTimes(1);
expect(mockHttpRequestFunction).toHaveBeenCalledWith(context, {
method: "POST",
url: params.location,
follows: ["completion"],
url: "/task/tasks/HiItsMeLocation/completionState",
data: {
complete: true
}
Expand All @@ -52,4 +51,28 @@ describe("completeTaskFactory", () => {
expect(mockTransformFunction).toHaveBeenCalledTimes(1);
expect(mockTransformFunction).toHaveBeenCalledWith(response, context, params);
});

it("invalid location should throw an error", async () => {
const completeTask = _completeTaskFactory(mockHttpRequestFunction, mockTransformFunction);
await expect(() => completeTask(context, {
location: "/some/faulty/location"
})).rejects.toThrow(TaskError);
});

it("should work with location with request parameters", async () => {

const completeTask = _completeTaskFactory(mockHttpRequestFunction, mockTransformFunction);
await completeTask(context, {
location: "/task/tasks/HiItsMeLocation?foo=bar&baz=foo"
});

expect(mockHttpRequestFunction).toHaveBeenCalledTimes(1);
expect(mockHttpRequestFunction).toHaveBeenCalledWith(context, {
method: "POST",
url: "/task/tasks/HiItsMeLocation/completionState",
data: {
complete: true
}
});
});
});
25 changes: 15 additions & 10 deletions packages/task/src/tasks/complete-task/complete-task.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DvelopContext } from "@dvelop-sdk/core";
import { HttpConfig, HttpResponse, _defaultHttpRequestFunction } from "../../utils/http";
import {HttpConfig, HttpResponse, _defaultHttpRequestFunction, TaskError} from "../../utils/http";

/**
* Parameters for the {@link completeTask}-function.
Expand All @@ -21,16 +21,21 @@ export function _completeTaskFactory<T>(
transformFunction: (response: HttpResponse, context: DvelopContext, params: CompleteTaskParams) => T,
): (context: DvelopContext, params: CompleteTaskParams) => Promise<T> {
return async (context: DvelopContext, params: CompleteTaskParams) => {
const matches: RegExpExecArray | null = /^\/task\/tasks\/(?<id>[^?]*)\??.*$/i.exec(params.location);
if (matches) {
const id = matches.groups?.id;

const response: HttpResponse = await httpRequestFunction(context, {
method: "POST",
url: params.location,
follows: ["completion"],
data: {
complete: true
}
});
return transformFunction(response, context, params);
const response: HttpResponse = await httpRequestFunction(context, {
method: "POST",
url: `/task/tasks/${id}/completionState`,
data: {
complete: true
}
});
return transformFunction(response, context, params);
} else {
throw new TaskError(`Failed to parse task id from '${params.location}'`);
}
};
}

Expand Down

0 comments on commit 292df49

Please sign in to comment.