Skip to content

Commit 54cfd51

Browse files
authored
fix(api-audit-logs): compression (#4617)
1 parent 0d50686 commit 54cfd51

File tree

4 files changed

+59
-9
lines changed

4 files changed

+59
-9
lines changed

packages/api-aco/src/record/record.so.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ export const createSearchRecordOperations = (
4646
}
4747
});
4848

49-
return [entries.map(pickEntryFieldValues<SearchRecord<any>>), meta];
49+
const items = entries.map(pickEntryFieldValues<SearchRecord<any>>);
50+
51+
return [items, meta];
5052
},
5153
async listTags(model, params) {
5254
const { where } = params;

packages/api-audit-logs/__tests__/createAuditLog.test.ts

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ import { AuditAction } from "~/types";
33
import { useHandler } from "./helpers/useHandler";
44
import { ActionType } from "~/config";
55
import { AUDIT_LOGS_TYPE } from "~/app/contants";
6-
import { compressor } from "~/utils/compressor";
6+
import { getDocumentClient } from "@webiny/project-utils/testing/dynamodb/index.js";
7+
import { parseIdentifier } from "@webiny/utils/parseIdentifier.js";
78

89
describe("create audit log", () => {
10+
const client = getDocumentClient();
11+
912
const audit: AuditAction = {
1013
app: {
1114
app: "cms",
@@ -37,6 +40,8 @@ describe("create audit log", () => {
3740
};
3841

3942
it("should create a new audit log", async () => {
43+
expect.assertions(3);
44+
4045
const createAuditLog = getAuditConfig(audit);
4146

4247
const { handler } = useHandler();
@@ -66,17 +71,40 @@ describe("create audit log", () => {
6671
timestamp: expect.any(Date),
6772
entityId,
6873
message,
69-
data: await compressor.compress(JSON.stringify(data))
74+
data: JSON.stringify(data)
7075
},
7176
location: {
7277
folderId: "root"
7378
},
7479
tags: [],
7580
type: "AuditLogs"
7681
});
82+
83+
// @ts-expect-error
84+
const partitionKey = `#CME#wby-aco-${result!.id}`;
85+
86+
const scanned = await client.scan({
87+
TableName: process.env.DB_TABLE
88+
});
89+
90+
for (const item of scanned.Items || []) {
91+
if (item.PK.includes(partitionKey) === false) {
92+
continue;
93+
}
94+
expect(item).toMatchObject({
95+
PK: expect.stringContaining(partitionKey),
96+
values: {
97+
"object@data": {
98+
"text@data": expect.stringMatching(`{"compression":"gzip","value":`)
99+
}
100+
}
101+
});
102+
}
77103
});
78104

79105
it("should list created logs", async () => {
106+
expect.assertions(3);
107+
80108
const createAuditLog = getAuditConfig(audit);
81109

82110
const { handler } = useHandler();
@@ -117,5 +145,25 @@ describe("create audit log", () => {
117145
tags: [],
118146
type: "AuditLogs"
119147
});
148+
149+
const { id: partitionKey } = parseIdentifier(`${result!.id}`);
150+
151+
const scanned = await client.scan({
152+
TableName: process.env.DB_TABLE
153+
});
154+
155+
for (const item of scanned.Items || []) {
156+
if (item.PK.includes(partitionKey) === false) {
157+
continue;
158+
}
159+
expect(item).toMatchObject({
160+
PK: expect.stringContaining(partitionKey),
161+
values: {
162+
"object@data": {
163+
"text@data": expect.stringMatching(`{"compression":"gzip","value":`)
164+
}
165+
}
166+
});
167+
}
120168
});
121169
});

packages/api-audit-logs/src/app/app.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const toDate = (value: string | Date) => {
1414
}
1515
};
1616

17-
const decompressData = (entry: SearchRecord<any>): SearchRecord<any> => {
17+
const decompressData = async (entry: SearchRecord<any>): Promise<SearchRecord<any>> => {
1818
if (!entry.data?.data) {
1919
return entry;
2020
}
@@ -23,7 +23,7 @@ const decompressData = (entry: SearchRecord<any>): SearchRecord<any> => {
2323
data: {
2424
...entry.data,
2525
timestamp: toDate(entry.data.timestamp),
26-
data: compressor.decompress(entry.data.data)
26+
data: await compressor.decompress(entry.data.data)
2727
}
2828
};
2929
};
@@ -104,8 +104,8 @@ export const createApp = (): IAcoAppRegisterParams => {
104104
return decompressData(entry);
105105
},
106106
onEntryList: async entries => {
107-
return Promise.all(
108-
entries.map(entry => {
107+
return await Promise.all(
108+
entries.map(async entry => {
109109
return decompressData(entry);
110110
})
111111
);

packages/api-audit-logs/src/utils/compressor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ class GzipCompression implements Compressor {
2828
try {
2929
const result = JSON.parse(data);
3030
if (!result?.compression) {
31-
return false;
31+
return true;
3232
}
3333
compression = result.compression;
3434
} catch {
35-
return false;
35+
return true;
3636
}
3737
/**
3838
* If already compressed, skip this.

0 commit comments

Comments
 (0)