From ce3248562bf9558de3844f4d15c6c9ef21cd15d2 Mon Sep 17 00:00:00 2001 From: Michael Zetterberg Date: Tue, 27 Aug 2024 10:55:46 +0200 Subject: [PATCH] feat: mask date of birth --- server/routers/user/query.ts | 3 +++ utils/maskValue.ts | 12 ++++++++++-- utils/maskvalue.test.ts | 7 ++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/server/routers/user/query.ts b/server/routers/user/query.ts index bda5a8744..287ebee0b 100644 --- a/server/routers/user/query.ts +++ b/server/routers/user/query.ts @@ -331,6 +331,9 @@ export const userQueryRouter = router({ user.address.zipCode = verifiedData.data.address?.zipCode ? maskValue.text(verifiedData.data.address.zipCode) : "" + + user.dateOfBirth = maskValue.all(user.dateOfBirth) + user.email = maskValue.email(user.email) user.phoneNumber = user.phoneNumber diff --git a/utils/maskValue.ts b/utils/maskValue.ts index 2f0f1bc75..7c3e0b50c 100644 --- a/utils/maskValue.ts +++ b/utils/maskValue.ts @@ -1,7 +1,11 @@ +function maskAll(str: string) { + return "*".repeat(str.length) +} + function maskAllButFirstChar(str: string) { const first = str[0] const rest = str.substring(1) - const restMasked = "*".repeat(rest.length) + const restMasked = maskAll(rest) return `${first}${restMasked}` } @@ -9,7 +13,7 @@ function maskAllButFirstChar(str: string) { function maskAllButLastTwoChar(str: string) { const lastTwo = str.slice(-2) const rest = str.substring(0, str.length - 2) - const restMasked = "*".repeat(rest.length) + const restMasked = maskAll(rest) return `${restMasked}${lastTwo}` } @@ -42,3 +46,7 @@ export function phone(str: string) { export function text(str: string) { return maskAllButFirstChar(str) } + +export function all(str: string) { + return maskAll(str) +} diff --git a/utils/maskvalue.test.ts b/utils/maskvalue.test.ts index 28e55fa38..0b147286e 100644 --- a/utils/maskvalue.test.ts +++ b/utils/maskvalue.test.ts @@ -1,6 +1,6 @@ import { describe, expect, test } from "@jest/globals" -import { email, phone, text } from "./maskValue" +import { all, email, phone, text } from "./maskValue" describe("Mask value", () => { test("masks e-mails properly", () => { @@ -19,4 +19,9 @@ describe("Mask value", () => { expect(text("test")).toBe("t***") expect(text("test.with.dot")).toBe("t************") }) + + test("masks whole string properly", () => { + expect(all("test")).toBe("****") + expect(all("123jknasd@iajsd.c")).toBe("*****************") + }) })