Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Included x, y, width, height parameters in creation and updation of notes using PUT request #255

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,20 @@ Payload:
}
```

or

```js
{
"x": "integer",
"y": "integer",
"type": "note",
"text": "text on note",
"color": "hexadecimal color code",
"width": "integer",
"height": "integer",
}
```

### GET /api/v1/board/:boardId

Return board current state as JSON.
Expand Down
4 changes: 4 additions & 0 deletions backend/src/api/api-routes.openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,14 @@ const spec: { paths: OpenAPIV3.PathsObject } = {
type: "object",
required: ["type", "text", "color"],
properties: {
x: { type: "number" },
y: { type: "number" },
type: { type: "string", enum: ["note"] },
text: { type: "string" },
color: { type: "string" },
container: { type: "string" },
width: { type: "number" },
height: { type: "number" },
replaceTextIfExists: { type: "boolean" },
replaceColorIfExists: { type: "boolean" },
replaceContainerIfExists: { type: "boolean" },
Expand Down
48 changes: 40 additions & 8 deletions backend/src/api/item-create-or-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ export const itemCreateOrUpdate = route
color: t.string,
}),
t.partial({
x: t.number,
y: t.number,
container: t.string,
width: t.number,
height: t.number,
replaceTextIfExists: t.boolean,
replaceColorIfExists: t.boolean,
replaceContainerIfExists: t.boolean,
Expand All @@ -46,10 +50,14 @@ export const itemCreateOrUpdate = route
checkBoardAPIAccess(request, async (board) => {
const { itemId } = request.routeParams
let {
x,
y,
type,
text,
color,
container,
width,
height,
replaceTextIfExists,
replaceColorIfExists,
replaceContainerIfExists = true,
Expand All @@ -59,51 +67,75 @@ export const itemCreateOrUpdate = route
if (existingItem) {
updateItem(
board,
x ?? existingItem.x,
y ?? existingItem.y,
type,
text,
color,
container,
width ?? existingItem.width,
height ?? existingItem.height,
itemId,
replaceTextIfExists,
replaceColorIfExists,
replaceContainerIfExists,
)
} else {
console.log(`Adding new item`)
addItem(board, type, text, color, container, itemId)
const partialParams = { x, y, width, height }
if (x !== undefined || y !== undefined || width !== undefined || height !== undefined) {
addItem(board, type, text, color, container, itemId, partialParams)
} else {
addItem(board, type, text, color, container, itemId)
}
}
return ok({ ok: true })
}),
)

function updateItem(
board: ServerSideBoardState,
x: number,
y: number,
type: "note",
text: string,
color: Color,
container: string | undefined,
width: number,
height: number,
itemId: string,
replaceTextIfExists: boolean | undefined,
replaceColorIfExists: boolean | undefined,
replaceContainerIfExists: boolean | undefined,
) {
const existingItem = board.board.items[itemId]

if (!isNote(existingItem)) {
throw new InvalidRequest("Unexpected item type")
}
const containerItem = findContainer(container, board.board)
const currentContainer = findContainer(existingItem.containerId, board.board)
const containerAttrs =
replaceContainerIfExists && containerItem !== currentContainer
? getItemAttributesForContainer(container, board.board)
: {}

let updatedItem: Note = {
...existingItem,
...containerAttrs,
x: x !== undefined ? x : existingItem.x,
y: y !== undefined ? y : existingItem.y,
text: replaceTextIfExists !== false ? text : existingItem.text,
color: replaceColorIfExists !== false ? color || existingItem.color : existingItem.color,
width: width !== undefined ? width : existingItem.width,
height: height !== undefined ? height : existingItem.height,
}

if (container && replaceContainerIfExists !== false) {
const containerItem = findContainer(container, board.board)
const currentContainer = findContainer(existingItem.containerId, board.board)
const containerAttrs =
containerItem !== currentContainer ? getItemAttributesForContainer(container, board.board) : {}

updatedItem = {
...updatedItem,
...containerAttrs,
}
}

if (!_.isEqual(updatedItem, existingItem)) {
console.log(`Updating existing item`)
dispatchSystemAppEvent(board, { action: "item.update", boardId: board.board.id, items: [updatedItem] })
Expand Down
6 changes: 6 additions & 0 deletions backend/src/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,19 @@ export function addItem(
color: Color,
container: string | undefined,
itemId?: string,
partialParams?: Partial<{ x: number; y: number; width: number; height: number }>,
) {
if (type !== "note") throw new InvalidRequest("Expecting type: note")
if (typeof text !== "string" || text.length === 0) throw new InvalidRequest("Expecting non zero-length text")

let itemAttributes: object = getItemAttributesForContainer(container, board.board)
if (itemId) itemAttributes = { ...itemAttributes, id: itemId }

// Merge partial parameters with existing attributes
if (partialParams) {
itemAttributes = { ...itemAttributes, ...partialParams }
}

const item: Note = { ...newNote(text, color || DEFAULT_NOTE_COLOR), ...itemAttributes }
const appEvent: AppEvent = { action: "item.add", boardId: board.board.id, items: [item], connections: [] }
dispatchSystemAppEvent(board, appEvent)
Expand Down
17 changes: 17 additions & 0 deletions playwright/src/tests/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@
await board.assertItemPosition(board.getNote("Updated item"), 613, 460)
})

const itemnew = await Api.createNote(accessToken, id, "API New note")

await expect(board.getNote("API New note")).toBeVisible()

await test.step("Update new item", async () => {
await Api.updateItem(accessToken, id, itemnew.id, {
x: 20,
y: 20,
type: "note",
text: "Updated new item",
color: "#000000",
width: 10,
height: 10,
})
await expect(board.getNote("Updated new item")).toBeVisible()
})

await test.step("Get board state", async () => {
const content = await Api.getBoard(accessToken, id)
expect(content).toEqual({
Expand Down Expand Up @@ -100,7 +117,7 @@
})

await test.step("Get board as CSV", async () => {
expect(await Api.getBoardCsv(accessToken, id)).toEqual("More API notes,Updated item\n")

Check failure on line 120 in playwright/src/tests/api.spec.ts

View workflow job for this annotation

GitHub Actions / test (18.x)

[chromium] › src/tests/api.spec.ts:8:9 › API endpoints › Create and update board

1) [chromium] › src/tests/api.spec.ts:8:9 › API endpoints › Create and update board › Get board as CSV Error: expect(received).toEqual(expected) // deep equality - Expected - 1 + Received + 2 - More API notes,Updated item + More API notes,Updated item + API notes,Updated new item ↵ 118 | 119 | await test.step("Get board as CSV", async () => { > 120 | expect(await Api.getBoardCsv(accessToken, id)).toEqual("More API notes,Updated item\n") | ^ 121 | }) 122 | 123 | await test.step("Set accessPolicy", async () => { at /home/runner/work/ourboard/ourboard/playwright/src/tests/api.spec.ts:120:60 at /home/runner/work/ourboard/ourboard/playwright/src/tests/api.spec.ts:119:9

Check failure on line 120 in playwright/src/tests/api.spec.ts

View workflow job for this annotation

GitHub Actions / test (18.x)

[chromium] › src/tests/api.spec.ts:8:9 › API endpoints › Create and update board

1) [chromium] › src/tests/api.spec.ts:8:9 › API endpoints › Create and update board › Get board as CSV Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(received).toEqual(expected) // deep equality - Expected - 1 + Received + 2 - More API notes,Updated item + More API notes,Updated item + API notes,Updated new item ↵ 118 | 119 | await test.step("Get board as CSV", async () => { > 120 | expect(await Api.getBoardCsv(accessToken, id)).toEqual("More API notes,Updated item\n") | ^ 121 | }) 122 | 123 | await test.step("Set accessPolicy", async () => { at /home/runner/work/ourboard/ourboard/playwright/src/tests/api.spec.ts:120:60 at /home/runner/work/ourboard/ourboard/playwright/src/tests/api.spec.ts:119:9

Check failure on line 120 in playwright/src/tests/api.spec.ts

View workflow job for this annotation

GitHub Actions / test (18.x)

[chromium] › src/tests/api.spec.ts:8:9 › API endpoints › Create and update board

1) [chromium] › src/tests/api.spec.ts:8:9 › API endpoints › Create and update board › Get board as CSV Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(received).toEqual(expected) // deep equality - Expected - 1 + Received + 2 - More API notes,Updated item + More API notes,Updated item + API notes,Updated new item ↵ 118 | 119 | await test.step("Get board as CSV", async () => { > 120 | expect(await Api.getBoardCsv(accessToken, id)).toEqual("More API notes,Updated item\n") | ^ 121 | }) 122 | 123 | await test.step("Set accessPolicy", async () => { at /home/runner/work/ourboard/ourboard/playwright/src/tests/api.spec.ts:120:60 at /home/runner/work/ourboard/ourboard/playwright/src/tests/api.spec.ts:119:9

Check failure on line 120 in playwright/src/tests/api.spec.ts

View workflow job for this annotation

GitHub Actions / test (18.x)

[firefox] › src/tests/api.spec.ts:8:9 › API endpoints › Create and update board

2) [firefox] › src/tests/api.spec.ts:8:9 › API endpoints › Create and update board › Get board as CSV Error: expect(received).toEqual(expected) // deep equality - Expected - 1 + Received + 2 - More API notes,Updated item + More API notes,Updated item + API notes,Updated new item ↵ 118 | 119 | await test.step("Get board as CSV", async () => { > 120 | expect(await Api.getBoardCsv(accessToken, id)).toEqual("More API notes,Updated item\n") | ^ 121 | }) 122 | 123 | await test.step("Set accessPolicy", async () => { at /home/runner/work/ourboard/ourboard/playwright/src/tests/api.spec.ts:120:60 at /home/runner/work/ourboard/ourboard/playwright/src/tests/api.spec.ts:119:9

Check failure on line 120 in playwright/src/tests/api.spec.ts

View workflow job for this annotation

GitHub Actions / test (18.x)

[firefox] › src/tests/api.spec.ts:8:9 › API endpoints › Create and update board

2) [firefox] › src/tests/api.spec.ts:8:9 › API endpoints › Create and update board › Get board as CSV Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(received).toEqual(expected) // deep equality - Expected - 1 + Received + 2 - More API notes,Updated item + More API notes,Updated item + API notes,Updated new item ↵ 118 | 119 | await test.step("Get board as CSV", async () => { > 120 | expect(await Api.getBoardCsv(accessToken, id)).toEqual("More API notes,Updated item\n") | ^ 121 | }) 122 | 123 | await test.step("Set accessPolicy", async () => { at /home/runner/work/ourboard/ourboard/playwright/src/tests/api.spec.ts:120:60 at /home/runner/work/ourboard/ourboard/playwright/src/tests/api.spec.ts:119:9

Check failure on line 120 in playwright/src/tests/api.spec.ts

View workflow job for this annotation

GitHub Actions / test (18.x)

[firefox] › src/tests/api.spec.ts:8:9 › API endpoints › Create and update board

2) [firefox] › src/tests/api.spec.ts:8:9 › API endpoints › Create and update board › Get board as CSV Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(received).toEqual(expected) // deep equality - Expected - 1 + Received + 2 - More API notes,Updated item + More API notes,Updated item + API notes,Updated new item ↵ 118 | 119 | await test.step("Get board as CSV", async () => { > 120 | expect(await Api.getBoardCsv(accessToken, id)).toEqual("More API notes,Updated item\n") | ^ 121 | }) 122 | 123 | await test.step("Set accessPolicy", async () => { at /home/runner/work/ourboard/ourboard/playwright/src/tests/api.spec.ts:120:60 at /home/runner/work/ourboard/ourboard/playwright/src/tests/api.spec.ts:119:9
})

await test.step("Set accessPolicy", async () => {
Expand Down
Loading