Skip to content

Commit

Permalink
Merge pull request #27 from shopware/fix-update-shops-in-dynamodb
Browse files Browse the repository at this point in the history
fix: do not override shops when updating or re-creating
  • Loading branch information
shyim authored Dec 19, 2024
2 parents d0225f1 + e1f1fcd commit df57a94
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
23 changes: 13 additions & 10 deletions src/integration/dynamodb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { describe, expect, mock, test } from "bun:test";
import { DynamoDBRepository } from "./dynamodb.js";

import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import type { DeleteCommand, PutCommand } from "@aws-sdk/lib-dynamodb";
import type {
DeleteCommand,
PutCommand,
UpdateCommand,
} from "@aws-sdk/lib-dynamodb";
import { SimpleShop } from "../repository.js";

describe("DynamoDB", async () => {
Expand Down Expand Up @@ -88,14 +92,14 @@ describe("DynamoDB", async () => {
});

test("updateShop", async () => {
let cmd: PutCommand;
let cmd: UpdateCommand;

mock.module("@aws-sdk/lib-dynamodb", () => {
return {
DynamoDBDocumentClient: {
from() {
return {
async send(inner: PutCommand) {
async send(inner: UpdateCommand) {
cmd = inner;
return {};
},
Expand All @@ -112,13 +116,12 @@ describe("DynamoDB", async () => {
// @ts-expect-error
expect(cmd).toBeDefined();
// @ts-expect-error
expect(cmd.input.Item).toEqual({
id: "a",
active: false,
url: "b",
secret: "c",
clientId: null,
clientSecret: null,
expect(cmd.input.ExpressionAttributeValues).toEqual({
":active": false,
":url": "b",
":secret": "c",
":clientId": null,
":clientSecret": null,
});
});

Expand Down
22 changes: 14 additions & 8 deletions src/integration/dynamodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DynamoDBDocumentClient,
GetCommand,
PutCommand,
UpdateCommand,
} from "@aws-sdk/lib-dynamodb";
import { type ShopRepositoryInterface, SimpleShop } from "../repository.js";

Expand Down Expand Up @@ -69,15 +70,20 @@ export class DynamoDBRepository implements ShopRepositoryInterface<SimpleShop> {
}

async updateShop(shop: SimpleShop): Promise<void> {
const cmd = new PutCommand({
const cmd = new UpdateCommand({
TableName: this.tableName,
Item: {
id: shop.getShopId(),
active: shop.getShopActive(),
url: shop.getShopUrl(),
secret: shop.getShopSecret(),
clientId: shop.getShopClientId(),
clientSecret: shop.getShopClientSecret(),
Key: { id: shop.getShopId() },
UpdateExpression:
"SET active = :active, #u = :url, secret = :secret, clientId = :clientId, clientSecret = :clientSecret",
ExpressionAttributeNames: {
"#u": "url",
},
ExpressionAttributeValues: {
":active": shop.getShopActive(),
":url": shop.getShopUrl(),
":secret": shop.getShopSecret(),
":clientId": shop.getShopClientId(),
":clientSecret": shop.getShopClientSecret(),
},
});

Expand Down

0 comments on commit df57a94

Please sign in to comment.