introduce TRPC and service layer

This commit is contained in:
Michael Dausmann
2023-02-05 11:55:49 +11:00
parent 791192a1ae
commit bba070d985
13 changed files with 307 additions and 58 deletions

View File

@@ -0,0 +1,29 @@
import { PrismaClient } from '@prisma/client';
export default class NotesService {
private prisma: PrismaClient;
constructor( prisma: PrismaClient) {
this.prisma = prisma;
}
async getAllNotes() {
return this.prisma.note.findMany();
}
async getNotesForAccountId(account_id: number) {
return this.prisma.note.findMany({ where: { account_id } });
}
async createNote( account_id: number, note_text: string ) {
return this.prisma.note.create({ data: { account_id, note_text }});
}
async updateNote(id: number, note_text: string) {
return this.prisma.note.update({ where: { id }, data: { note_text } });
}
async deleteNote(id: number) {
return this.prisma.note.delete({ where: { id } });
}
}

View File

@@ -0,0 +1,47 @@
import { PrismaClient } from '@prisma/client';
import { UtilService } from './util.service';
const TRIAL_PLAN_NAME = '3 Month Trial'; // TODO - some sort of config.. this will change for every use of the boilerplate
export default class UserService {
private prisma: PrismaClient;
constructor( prisma: PrismaClient) {
this.prisma = prisma;
}
async getUserBySupabaseId(supabase_uid: string) {
return this.prisma.user.findFirst({ where: { supabase_uid }, include: { membership: true } });
}
async getUserById(id: number) {
return this.prisma.user.findFirstOrThrow({ where: { id }, include: { membership: true } });
}
async createUser( supabase_uid: string, display_name: string ) {
const trialPlan = await this.prisma.plan.findFirstOrThrow({ where: { name: TRIAL_PLAN_NAME}});
return this.prisma.user.create({
data:{
supabase_uid: supabase_uid,
display_name: display_name,
membership: {
create: {
account: {
create: {
plan_id: trialPlan.id,
name: display_name,
features: trialPlan.features, //copy in features from the plan, plan_id is a loose association and settings can change independently
current_period_ends: UtilService.addMonths(new Date(),3),
}
}
}
}
},
include: { membership: true },
});
}
async deleteUser(id: number) {
return this.prisma.user.delete({ where: { id } });
}
}

View File

@@ -0,0 +1,10 @@
export class UtilService {
public static addMonths(date: Date, months: number): Date {
const d = date.getDate();
date.setMonth(date.getMonth() + +months);
if (date.getDate() != d) {
date.setDate(0);
}
return date;
}
}