From 412c1d23cd0879761399f3d3c1f07f60bf70b2c2 Mon Sep 17 00:00:00 2001 From: Eser Ozvataf Date: Wed, 23 Aug 2023 00:40:42 +0300 Subject: [PATCH] refactor: entity type for profile --- deno.jsonc | 2 +- pkg/api/data/seed.ts | 16 +++++++++------- pkg/api/functions/profile-get/mod.ts | 27 +++++++++++++++++++-------- pkg/protocol/languages.ts | 9 +++++++++ pkg/protocol/profile-get-result.ts | 4 ---- pkg/protocol/profile-link.ts | 4 ++-- pkg/protocol/profile-membership.ts | 4 ++-- pkg/protocol/profile-page.ts | 4 ++-- pkg/protocol/profile-story.ts | 4 ++-- pkg/protocol/profile.ts | 16 ++++++++++++---- pkg/protocol/result-type.ts | 2 +- pkg/protocol/user.ts | 4 ++-- 12 files changed, 61 insertions(+), 35 deletions(-) create mode 100644 pkg/protocol/languages.ts delete mode 100644 pkg/protocol/profile-get-result.ts diff --git a/deno.jsonc b/deno.jsonc index 5d26bde..d0bc8fe 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -15,7 +15,7 @@ "api:check": "deno run --unstable --check --reload ./pkg/api/mod.ts", "api:cli": "deno repl --unstable --allow-all --eval-file=./pkg/api/cli-init.ts", - "api:data-seed": "deno run --unstable --allow-all ./pkg/api/seed.ts", + "api:data-seed": "deno run --unstable --allow-all ./pkg/api/data/seed.ts", "api:deploy": "export $(grep -v '^#' .env.local | xargs -0) && deployctl deploy --project=$DENO_DEPLOY_PROJECT_ID ./pkg/api/main.ts", "api:dev": "deno run --unstable --allow-all ./pkg/api/dev.ts", diff --git a/pkg/api/data/seed.ts b/pkg/api/data/seed.ts index 766901a..bd45d32 100644 --- a/pkg/api/data/seed.ts +++ b/pkg/api/data/seed.ts @@ -1,23 +1,25 @@ import { ulid } from "@ulid"; -import { type Profile } from "@protocol/profile.ts"; +import { type ProfileEntity } from "@protocol/profile.ts"; import { Connection } from "@api/data/connection.ts"; export const seed = async () => { const kv = await Connection.instance.getKv(); - const profile: Profile = { + const profile: ProfileEntity = { id: ulid(), type: "Individual", slug: "eser", - title: "Eser Ozvataf", - description: "", profilePictureUri: null, + tx: { + tr: { + title: "Eser Ozvataf", + description: "", + }, + }, + showStories: true, showMembers: false, - - links: undefined, - pages: undefined, }; await kv.set(["profile", "eser"], profile); diff --git a/pkg/api/functions/profile-get/mod.ts b/pkg/api/functions/profile-get/mod.ts index 87fca88..b7e8a9f 100644 --- a/pkg/api/functions/profile-get/mod.ts +++ b/pkg/api/functions/profile-get/mod.ts @@ -1,20 +1,21 @@ import { z } from "@zod"; -import { type Profile } from "@protocol/profile.ts"; -import { type ProfileGetResult } from "@protocol/profile-get-result.ts"; +import { type LanguageCode } from "@protocol/languages.ts"; +import { type Profile, type ProfileEntity } from "@protocol/profile.ts"; +import { type ResultType } from "@protocol/result-type.ts"; import { Connection } from "@api/data/connection.ts"; export const profileGet = async ( slug: string, - lang: string, + lang: LanguageCode, ) => { const kv = await Connection.instance.getKv(); const slugValidated = await z.string().parseAsync(slug); - const profile = await kv.get(["profile", slugValidated]); + const kvRecord = await kv.get(["profile", slugValidated]); - if (profile.value === null) { - const result: ProfileGetResult = { + if (kvRecord.value === null) { + const result: ResultType = { payload: null, error: { message: `Profile not found for slug: ${slug} lang: ${lang}`, @@ -24,8 +25,18 @@ export const profileGet = async ( return result; } - const result: ProfileGetResult = { - payload: profile.value, + const profile: Profile = { + ...kvRecord.value, + + title: kvRecord.value.tx[lang]?.title ?? "", + description: kvRecord.value.tx[lang]?.description ?? "", + + links: undefined, + pages: undefined, + }; + + const result: ResultType = { + payload: profile, }; return result; diff --git a/pkg/protocol/languages.ts b/pkg/protocol/languages.ts new file mode 100644 index 0000000..c402877 --- /dev/null +++ b/pkg/protocol/languages.ts @@ -0,0 +1,9 @@ +export const languages = { + "tr": { name: "Türkçe", englishName: "Turkish" }, + "en": { name: "English", englishName: "English" }, +}; + +export const defaultLanguageCode = "tr"; + +export type LanguageCode = keyof typeof languages; +export type Language = typeof languages[typeof defaultLanguageCode]; diff --git a/pkg/protocol/profile-get-result.ts b/pkg/protocol/profile-get-result.ts deleted file mode 100644 index 95b71ad..0000000 --- a/pkg/protocol/profile-get-result.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { type ResultType } from "./result-type.ts"; -import { type Profile } from "./profile.ts"; - -export type ProfileGetResult = ResultType; diff --git a/pkg/protocol/profile-link.ts b/pkg/protocol/profile-link.ts index 1d58bec..414e523 100644 --- a/pkg/protocol/profile-link.ts +++ b/pkg/protocol/profile-link.ts @@ -1,4 +1,4 @@ -export type ProfileLink = { +export interface ProfileLink { id: string; slug: string; title: string; @@ -9,4 +9,4 @@ export type ProfileLink = { iconKey: string | null; order: number; -}; +} diff --git a/pkg/protocol/profile-membership.ts b/pkg/protocol/profile-membership.ts index f4c9d78..a15c49f 100644 --- a/pkg/protocol/profile-membership.ts +++ b/pkg/protocol/profile-membership.ts @@ -8,8 +8,8 @@ export type ProfileMembershipRole = | "Sponsor" | "Follower"; -export type ProfileMembership = { +export interface ProfileMembership { id: string; role: ProfileMembershipRole; user: User | null; -}; +} diff --git a/pkg/protocol/profile-page.ts b/pkg/protocol/profile-page.ts index 8fad6e7..1dad048 100644 --- a/pkg/protocol/profile-page.ts +++ b/pkg/protocol/profile-page.ts @@ -1,8 +1,8 @@ -export type ProfilePage = { +export interface ProfilePage { id: string; slug: string; title: string; content: string; order: number; publishedAt: string | null; -}; +} diff --git a/pkg/protocol/profile-story.ts b/pkg/protocol/profile-story.ts index 3b0a733..f694ffe 100644 --- a/pkg/protocol/profile-story.ts +++ b/pkg/protocol/profile-story.ts @@ -1,6 +1,6 @@ export type ProfileStoryCategory = "Status" | "Announcement" | "News"; -export type ProfileStory = { +export interface ProfileStory { id: string; slug: string; category: ProfileStoryCategory; @@ -10,4 +10,4 @@ export type ProfileStory = { content: string; publishedAt: string | null; -}; +} diff --git a/pkg/protocol/profile.ts b/pkg/protocol/profile.ts index 9e28355..b133f59 100644 --- a/pkg/protocol/profile.ts +++ b/pkg/protocol/profile.ts @@ -1,20 +1,28 @@ import { type ProfileLink } from "./profile-link.ts"; import { type ProfilePage } from "./profile-page.ts"; +import { type LanguageCode } from "./languages.ts"; export type ProfileType = "Individual" | "Organization" | "Product" | "Venue"; -export type Profile = { +export interface ProfileTx { + title: string; + description: string; +} + +export interface ProfileEntity { id: string; type: ProfileType; slug: string; - title: string; - description: string; profilePictureUri: string | null; + tx: Partial>; + showStories: boolean; showMembers: boolean; +} +export interface Profile extends Omit, ProfileTx { links: Array | undefined; pages: Array | undefined; // stories: Array | undefined; -}; +} diff --git a/pkg/protocol/result-type.ts b/pkg/protocol/result-type.ts index f24aec5..6b233a0 100644 --- a/pkg/protocol/result-type.ts +++ b/pkg/protocol/result-type.ts @@ -1,5 +1,5 @@ export interface ResultType { - payload: T; + payload: T | null; error?: { message: string; }; diff --git a/pkg/protocol/user.ts b/pkg/protocol/user.ts index b9f08f2..82020e7 100644 --- a/pkg/protocol/user.ts +++ b/pkg/protocol/user.ts @@ -1,6 +1,6 @@ import { type Profile } from "./profile.ts"; -export type User = { +export interface User { id: string; // email: string; fullname: string; @@ -10,4 +10,4 @@ export type User = { twitterHandle: string; individualProfile: Profile | null; -}; +}