-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
184 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
FROM node:alpine as builder | ||
WORKDIR /app | ||
COPY --chown=node:node package.json yarn.lock ./ | ||
RUN yarn install --frozen-lockfile | ||
COPY --chown=node:node . . | ||
RUN yarn build | ||
|
||
|
||
# Dev stage | ||
FROM builder as dev | ||
WORKDIR /app | ||
EXPOSE 3000 | ||
CMD ["yarn", "dev:global"] | ||
|
||
|
||
# Prod stage | ||
FROM node:alpine as prod | ||
WORKDIR /app | ||
COPY --from=builder /app/node_modules ./dist/node_modules | ||
COPY --from=builder /app/dist/src ./dist/src | ||
CMD ["node", "dist/src/services/global/server.js"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export type UserSettings = Partial<{ | ||
id: string | ||
userId: string | ||
plan: string | ||
}> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import supabase from "../../storage/supabase" | ||
import {listenSettings} from '../../storage/settings.storage' | ||
import { MissingData } from "../../exceptions/exceptions" | ||
import { updateUserCredits } from "../../storage/credits.storage" | ||
import { creditsPlanMapping } from "../../config" | ||
|
||
export const listenEvents = () => { | ||
listenSettingsForFreePlanCredits() | ||
} | ||
|
||
const listenSettingsForFreePlanCredits = () => { | ||
console.log('[EVENT][SETTINGS] Listening for user settings events...') | ||
listenSettings(async (change) => { | ||
// New user has been created | ||
if (change.eventType === 'INSERT') { | ||
const { id, userId, plan } = change.new | ||
if (!userId) { | ||
throw new MissingData('userId', `user settings ${id}`) | ||
} | ||
if (!plan) { | ||
throw new MissingData('plan', `user settings ${id}`) | ||
} | ||
|
||
// Credits amount are handled by Stripe webhook, but we need to handle Free plan registration | ||
if (plan === 'free') { | ||
const credits = creditsPlanMapping[plan] | ||
await updateUserCredits(userId, credits) | ||
console.log(`[EVENTS][SETTINGS] Set ${credits} credits for FREE plan to ${userId}`) | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { listenEvents } from "./events"; | ||
|
||
listenEvents() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import supabaseClient from "./supabase" | ||
|
||
export const getUserCredits = async (userId: string): Promise<number> => { | ||
return (await supabaseClient.from('user_credits').select('remaningCredits').eq('userId', userId).maybeSingle()).data?.remaningCredits || 0 | ||
} | ||
|
||
export const updateUserCredits = async (userId: string, remaningCredits: number): Promise<void> => { | ||
const res = await supabaseClient.from('user_credits').upsert({ userId, remaningCredits }).eq('userId', userId) | ||
if (res.error) { | ||
console.error(res.error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { RealtimePostgresChangesPayload } from "@supabase/supabase-js" | ||
import supabaseClient from './supabase' | ||
import { UserSettings } from "../models/settings" | ||
|
||
export const listenSettings = (onChange: (payload: RealtimePostgresChangesPayload<UserSettings>) => void) => { | ||
supabaseClient.channel('user_settings') | ||
.on('postgres_changes', { event: '*', schema: 'public', table: 'user_settings' }, onChange) | ||
.subscribe() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
create table "public"."user_credits" ( | ||
"id" bigint generated by default as identity not null, | ||
"userId" uuid, | ||
"remaningCredits" smallint default '0'::smallint | ||
); | ||
|
||
|
||
alter table "public"."user_credits" enable row level security; | ||
|
||
CREATE UNIQUE INDEX user_credits_pkey ON public.user_credits USING btree (id); | ||
|
||
alter table "public"."user_credits" add constraint "user_credits_pkey" PRIMARY KEY using index "user_credits_pkey"; | ||
|
||
alter table "public"."user_credits" add constraint "user_credits_userId_fkey" FOREIGN KEY ("userId") REFERENCES auth.users(id) ON DELETE CASCADE not valid; | ||
|
||
alter table "public"."user_credits" validate constraint "user_credits_userId_fkey"; | ||
|
||
create policy "Enable select for user" | ||
on "public"."user_credits" | ||
as permissive | ||
for select | ||
to authenticated | ||
using ((auth.uid() = "userId")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
alter table "public"."user_credits" drop constraint "user_credits_pkey"; | ||
|
||
drop index if exists "public"."user_credits_pkey"; | ||
|
||
alter table "public"."user_credits" drop column "id"; | ||
|
||
alter table "public"."user_credits" alter column "userId" set not null; | ||
|
||
CREATE UNIQUE INDEX "user_credits_userId_key" ON public.user_credits USING btree ("userId"); | ||
|
||
CREATE UNIQUE INDEX user_credits_pkey ON public.user_credits USING btree ("userId"); | ||
|
||
alter table "public"."user_credits" add constraint "user_credits_pkey" PRIMARY KEY using index "user_credits_pkey"; | ||
|
||
alter table "public"."user_credits" add constraint "user_credits_userId_key" UNIQUE using index "user_credits_userId_key"; |