Skip to content

Commit

Permalink
[#172621882] use yaml as output format for data (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
gunzip authored May 19, 2020
1 parent 5ed0e05 commit 2ec3768
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 15 deletions.
8 changes: 5 additions & 3 deletions ExtractUserDataActivity/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Either, right } from "fp-ts/lib/Either";
import { fromNullable, Option, some } from "fp-ts/lib/Option";

import * as stream from "stream";

import * as yaml from "yaml";
import * as zipstream from "../../utils/zip";

import { context as contextMock } from "../../__mocks__/durable-functions";
Expand Down Expand Up @@ -159,10 +159,12 @@ describe("createExtractUserDataActivityHandler", () => {
await handler(contextMock, input);

expect(aZipStream.finalize).toHaveBeenCalledTimes(1);
const allUserData: AllUserData = JSON.parse(
const allUserData: AllUserData = yaml.parse(
appendSpy.mock.calls[0][0].toString()
);
expect(allUserData.notifications[0].channels.WEBHOOK).toEqual({});
expect(allUserData.notifications[0].channels.WEBHOOK).toEqual({
url: null
});
});

it("should query using correct data", async () => {
Expand Down
36 changes: 26 additions & 10 deletions ExtractUserDataActivity/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ import { AllUserData, MessageContentWithId } from "../utils/userData";
import { getEncryptedZipStream } from "../utils/zip";
import { NotificationModel } from "./notification";

import * as yaml from "yaml";

export const ArchiveInfo = t.interface({
blobName: NonEmptyString,
password: StrongPassword
Expand Down Expand Up @@ -427,7 +429,7 @@ export const queryAllUserData = (
notifications
),
notifications: taskEither.of(notifications),
profile: taskEither.of(profile)
profiles: taskEither.of([profile])
});
}
);
Expand Down Expand Up @@ -462,10 +464,9 @@ export const saveDataToBlob = (
data: AllUserData,
password: StrongPassword
): TaskEither<ActivityResultArchiveGenerationFailure, ArchiveInfo> => {
const blobName = `${
data.profile.fiscalCode
}-${Date.now()}.zip` as NonEmptyString;
const fileName = `${data.profile.fiscalCode}.json` as NonEmptyString;
const profile = data.profiles[0];
const blobName = `${profile.fiscalCode}-${Date.now()}.zip` as NonEmptyString;
const fileName = `${profile.fiscalCode}.yaml` as NonEmptyString;

const zipStream = getEncryptedZipStream(password);

Expand All @@ -486,7 +487,7 @@ export const saveDataToBlob = (
)(userDataContainerName, blobName);

zipStream.pipe(blobStream);
zipStream.append(JSON.stringify(data), {
zipStream.append(yaml.stringify(data), {
name: fileName
});

Expand Down Expand Up @@ -525,6 +526,12 @@ export interface IActivityHandlerInput {
userDataContainerName: NonEmptyString;
}

// tslint:disable-next-line: no-any
const cleanData = (v: any) => {
const { _self, _etag, _attachments, _rid, _ts, ...clean } = v;
return clean;
};

/**
* Factory methods that builds an activity function
*/
Expand Down Expand Up @@ -564,11 +571,20 @@ export function createExtractUserDataActivityHandler({
)
.map(allUserData => {
// remove sensitive data
allUserData.notifications.forEach(e => {
// tslint:disable-next-line: no-object-mutation
e.channels.WEBHOOK = { url: undefined };
const notifications = allUserData.notifications.map(e => {
return cleanData({
...e,
channels: { ...e.channels, WEBHOOK: { url: undefined } }
});
});
return allUserData;
return {
messageContents: allUserData.messageContents,
messageStatuses: allUserData.messageStatuses.map(cleanData),
messages: allUserData.messages.map(cleanData),
notificationStatuses: allUserData.messageStatuses.map(cleanData),
notifications,
profiles: allUserData.profiles.map(cleanData)
} as AllUserData;
})
.chain(allUserData =>
saveDataToBlob(
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
"italia-ts-commons": "^8.1.0",
"randomstring": "^1.1.5",
"ulid": "^2.3.0",
"winston": "^3.2.1"
"winston": "^3.2.1",
"yaml": "^1.10.0"
},
"resolutions": {
"handlebars": "~4.5.3",
Expand Down
2 changes: 1 addition & 1 deletion utils/userData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ export const AllUserData = t.interface({
"NotificationStatusList"
),
notifications: t.readonlyArray(t.exact(SafeNotification), "NotificationList"),
profile: Profile
profiles: t.readonlyArray(t.exact(Profile))
});
export type AllUserData = t.TypeOf<typeof AllUserData>;
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9640,6 +9640,11 @@ yaml@1.8.2, yaml@^1.7.2:
dependencies:
"@babel/runtime" "^7.8.7"

yaml@^1.10.0:
version "1.10.0"
resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.10.0.tgz#3b593add944876077d4d683fee01081bd9fff31e"
integrity sha512-yr2icI4glYaNG+KWONODapy2/jDdMSDnrONSjblABjD9B4Z5LgiircSt8m8sRZFNi08kG9Sm0uSHtEmP3zaEGg==

yargs-parser@10.x:
version "10.1.0"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-10.1.0.tgz#7202265b89f7e9e9f2e5765e0fe735a905edbaa8"
Expand Down

0 comments on commit 2ec3768

Please sign in to comment.