Skip to content

Commit

Permalink
refactor: entity type for profile
Browse files Browse the repository at this point in the history
  • Loading branch information
eser committed Aug 22, 2023
1 parent 2968f2a commit 412c1d2
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 35 deletions.
2 changes: 1 addition & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -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",

Expand Down
16 changes: 9 additions & 7 deletions pkg/api/data/seed.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
27 changes: 19 additions & 8 deletions pkg/api/functions/profile-get/mod.ts
Original file line number Diff line number Diff line change
@@ -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>(["profile", slugValidated]);
const kvRecord = await kv.get<ProfileEntity>(["profile", slugValidated]);

if (profile.value === null) {
const result: ProfileGetResult = {
if (kvRecord.value === null) {
const result: ResultType<Profile> = {
payload: null,
error: {
message: `Profile not found for slug: ${slug} lang: ${lang}`,
Expand All @@ -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<Profile> = {
payload: profile,
};

return result;
Expand Down
9 changes: 9 additions & 0 deletions pkg/protocol/languages.ts
Original file line number Diff line number Diff line change
@@ -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];
4 changes: 0 additions & 4 deletions pkg/protocol/profile-get-result.ts

This file was deleted.

4 changes: 2 additions & 2 deletions pkg/protocol/profile-link.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type ProfileLink = {
export interface ProfileLink {
id: string;
slug: string;
title: string;
Expand All @@ -9,4 +9,4 @@ export type ProfileLink = {
iconKey: string | null;

order: number;
};
}
4 changes: 2 additions & 2 deletions pkg/protocol/profile-membership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export type ProfileMembershipRole =
| "Sponsor"
| "Follower";

export type ProfileMembership = {
export interface ProfileMembership {
id: string;
role: ProfileMembershipRole;
user: User | null;
};
}
4 changes: 2 additions & 2 deletions pkg/protocol/profile-page.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export type ProfilePage = {
export interface ProfilePage {
id: string;
slug: string;
title: string;
content: string;
order: number;
publishedAt: string | null;
};
}
4 changes: 2 additions & 2 deletions pkg/protocol/profile-story.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type ProfileStoryCategory = "Status" | "Announcement" | "News";

export type ProfileStory = {
export interface ProfileStory {
id: string;
slug: string;
category: ProfileStoryCategory;
Expand All @@ -10,4 +10,4 @@ export type ProfileStory = {
content: string;

publishedAt: string | null;
};
}
16 changes: 12 additions & 4 deletions pkg/protocol/profile.ts
Original file line number Diff line number Diff line change
@@ -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<Record<LanguageCode, ProfileTx>>;

showStories: boolean;
showMembers: boolean;
}

export interface Profile extends Omit<ProfileEntity, "tx">, ProfileTx {
links: Array<ProfileLink> | undefined;
pages: Array<ProfilePage> | undefined;
// stories: Array<ProfileStory> | undefined;
};
}
2 changes: 1 addition & 1 deletion pkg/protocol/result-type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface ResultType<T> {
payload: T;
payload: T | null;
error?: {
message: string;
};
Expand Down
4 changes: 2 additions & 2 deletions pkg/protocol/user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type Profile } from "./profile.ts";

export type User = {
export interface User {
id: string;
// email: string;
fullname: string;
Expand All @@ -10,4 +10,4 @@ export type User = {
twitterHandle: string;

individualProfile: Profile | null;
};
}

0 comments on commit 412c1d2

Please sign in to comment.