From 2ec3768cb304d4c1bac91d4fc48065802b792e6d Mon Sep 17 00:00:00 2001 From: Danilo Spinelli Date: Tue, 19 May 2020 14:32:36 +0200 Subject: [PATCH] [#172621882] use yaml as output format for data (#54) --- .../__tests__/handler.test.ts | 8 +++-- ExtractUserDataActivity/handler.ts | 36 +++++++++++++------ package.json | 3 +- utils/userData.ts | 2 +- yarn.lock | 5 +++ 5 files changed, 39 insertions(+), 15 deletions(-) diff --git a/ExtractUserDataActivity/__tests__/handler.test.ts b/ExtractUserDataActivity/__tests__/handler.test.ts index f2e886ce..2fc8c9c5 100644 --- a/ExtractUserDataActivity/__tests__/handler.test.ts +++ b/ExtractUserDataActivity/__tests__/handler.test.ts @@ -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"; @@ -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 () => { diff --git a/ExtractUserDataActivity/handler.ts b/ExtractUserDataActivity/handler.ts index e6f9d2b1..d0ebcd6a 100644 --- a/ExtractUserDataActivity/handler.ts +++ b/ExtractUserDataActivity/handler.ts @@ -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 @@ -427,7 +429,7 @@ export const queryAllUserData = ( notifications ), notifications: taskEither.of(notifications), - profile: taskEither.of(profile) + profiles: taskEither.of([profile]) }); } ); @@ -462,10 +464,9 @@ export const saveDataToBlob = ( data: AllUserData, password: StrongPassword ): TaskEither => { - 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); @@ -486,7 +487,7 @@ export const saveDataToBlob = ( )(userDataContainerName, blobName); zipStream.pipe(blobStream); - zipStream.append(JSON.stringify(data), { + zipStream.append(yaml.stringify(data), { name: fileName }); @@ -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 */ @@ -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( diff --git a/package.json b/package.json index 8e59d380..39933940 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/utils/userData.ts b/utils/userData.ts index c9988359..4e87ab18 100644 --- a/utils/userData.ts +++ b/utils/userData.ts @@ -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; diff --git a/yarn.lock b/yarn.lock index 13e4c025..c3079ec8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"