service refactor to namespaces
This commit is contained in:
@@ -14,15 +14,17 @@ import { AccountLimitError } from './errors';
|
||||
|
||||
const config = useRuntimeConfig();
|
||||
|
||||
export default class AccountService {
|
||||
async getAccountById(account_id: number): Promise<AccountWithMembers> {
|
||||
export namespace AccountService {
|
||||
export async function getAccountById(
|
||||
account_id: number
|
||||
): Promise<AccountWithMembers> {
|
||||
return prisma_client.account.findFirstOrThrow({
|
||||
where: { id: account_id },
|
||||
...accountWithMembers
|
||||
});
|
||||
}
|
||||
|
||||
async getAccountByJoinPassword(
|
||||
export async function getAccountByJoinPassword(
|
||||
join_password: string
|
||||
): Promise<AccountWithMembers> {
|
||||
return prisma_client.account.findFirstOrThrow({
|
||||
@@ -31,14 +33,16 @@ export default class AccountService {
|
||||
});
|
||||
}
|
||||
|
||||
async getAccountMembers(account_id: number): Promise<MembershipWithUser[]> {
|
||||
export async function getAccountMembers(
|
||||
account_id: number
|
||||
): Promise<MembershipWithUser[]> {
|
||||
return prisma_client.membership.findMany({
|
||||
where: { account_id },
|
||||
...membershipWithUser
|
||||
});
|
||||
}
|
||||
|
||||
async updateAccountStipeCustomerId(
|
||||
export async function updateAccountStipeCustomerId(
|
||||
account_id: number,
|
||||
stripe_customer_id: string
|
||||
) {
|
||||
@@ -50,7 +54,7 @@ export default class AccountService {
|
||||
});
|
||||
}
|
||||
|
||||
async updateStripeSubscriptionDetailsForAccount(
|
||||
export async function updateStripeSubscriptionDetailsForAccount(
|
||||
stripe_customer_id: string,
|
||||
stripe_subscription_id: string,
|
||||
current_period_ends: Date,
|
||||
@@ -93,7 +97,7 @@ export default class AccountService {
|
||||
}
|
||||
}
|
||||
|
||||
async acceptPendingMembership(
|
||||
export async function acceptPendingMembership(
|
||||
account_id: number,
|
||||
membership_id: number
|
||||
): Promise<MembershipWithAccount> {
|
||||
@@ -118,7 +122,7 @@ export default class AccountService {
|
||||
});
|
||||
}
|
||||
|
||||
async deleteMembership(
|
||||
export async function deleteMembership(
|
||||
account_id: number,
|
||||
membership_id: number
|
||||
): Promise<MembershipWithAccount> {
|
||||
@@ -140,7 +144,7 @@ export default class AccountService {
|
||||
});
|
||||
}
|
||||
|
||||
async joinUserToAccount(
|
||||
export async function joinUserToAccount(
|
||||
user_id: number,
|
||||
account_id: number,
|
||||
pending: boolean
|
||||
@@ -179,7 +183,10 @@ export default class AccountService {
|
||||
});
|
||||
}
|
||||
|
||||
async changeAccountName(account_id: number, new_name: string) {
|
||||
export async function changeAccountName(
|
||||
account_id: number,
|
||||
new_name: string
|
||||
) {
|
||||
return prisma_client.account.update({
|
||||
where: { id: account_id },
|
||||
data: {
|
||||
@@ -188,7 +195,7 @@ export default class AccountService {
|
||||
});
|
||||
}
|
||||
|
||||
async changeAccountPlan(account_id: number, plan_id: number) {
|
||||
export async function changeAccountPlan(account_id: number, plan_id: number) {
|
||||
const plan = await prisma_client.plan.findFirstOrThrow({
|
||||
where: { id: plan_id }
|
||||
});
|
||||
@@ -202,7 +209,7 @@ export default class AccountService {
|
||||
});
|
||||
}
|
||||
|
||||
async rotateJoinPassword(account_id: number) {
|
||||
export async function rotateJoinPassword(account_id: number) {
|
||||
const join_password: string = generator.generate({
|
||||
length: 10,
|
||||
numbers: true
|
||||
@@ -217,7 +224,7 @@ export default class AccountService {
|
||||
// User must already be an ADMIN for the Account
|
||||
// Existing OWNER memberships are downgraded to ADMIN
|
||||
// In future, some sort of Billing/Stripe tie in here e.g. changing email details on the Account, not sure.
|
||||
async claimOwnershipOfAccount(
|
||||
export async function claimOwnershipOfAccount(
|
||||
user_id: number,
|
||||
account_id: number
|
||||
): Promise<MembershipWithUser[]> {
|
||||
@@ -278,7 +285,7 @@ export default class AccountService {
|
||||
}
|
||||
|
||||
// Upgrade access of a membership. Cannot use this method to upgrade to or downgrade from OWNER access
|
||||
async changeUserAccessWithinAccount(
|
||||
export async function changeUserAccessWithinAccount(
|
||||
user_id: number,
|
||||
account_id: number,
|
||||
access: ACCOUNT_ACCESS
|
||||
@@ -333,15 +340,14 @@ export default class AccountService {
|
||||
Note.. for each usage limit, you will need another pair of check/increment methods and of course the count and max limit in the account schema
|
||||
|
||||
How to use in a service method....
|
||||
async someServiceMethod(account_id: number, .....etc) {
|
||||
const accountService = new AccountService();
|
||||
const account = await accountService.checkAIGenCount(account_id);
|
||||
export async function someServiceMethod(account_id: number, .....etc) {
|
||||
const account = await AccountService.checkAIGenCount(account_id);
|
||||
... User is under the limit so do work
|
||||
await accountService.incrementAIGenCount(account);
|
||||
await AccountService.incrementAIGenCount(account);
|
||||
}
|
||||
*/
|
||||
|
||||
async getAccountWithPeriodRollover(account_id: number) {
|
||||
export async function getAccountWithPeriodRollover(account_id: number) {
|
||||
const account = await prisma_client.account.findFirstOrThrow({
|
||||
where: { id: account_id }
|
||||
});
|
||||
@@ -366,8 +372,8 @@ export default class AccountService {
|
||||
return account;
|
||||
}
|
||||
|
||||
async checkAIGenCount(account_id: number) {
|
||||
const account = await this.getAccountWithPeriodRollover(account_id);
|
||||
export async function checkAIGenCount(account_id: number) {
|
||||
const account = await getAccountWithPeriodRollover(account_id);
|
||||
|
||||
if (account.ai_gen_count >= account.ai_gen_max_pm) {
|
||||
throw new AccountLimitError(
|
||||
@@ -378,7 +384,7 @@ export default class AccountService {
|
||||
return account;
|
||||
}
|
||||
|
||||
async incrementAIGenCount(account: any) {
|
||||
export async function incrementAIGenCount(account: any) {
|
||||
return await prisma_client.account.update({
|
||||
where: { id: account.id },
|
||||
data: {
|
||||
|
||||
@@ -6,8 +6,8 @@ import generator from 'generate-password-ts';
|
||||
|
||||
const config = useRuntimeConfig();
|
||||
|
||||
export default class AuthService {
|
||||
async getFullUserBySupabaseId(
|
||||
export namespace AuthService {
|
||||
export async function getFullUserBySupabaseId(
|
||||
supabase_uid: string
|
||||
): Promise<FullDBUser | null> {
|
||||
return prisma_client.user.findFirst({
|
||||
@@ -16,14 +16,16 @@ export default class AuthService {
|
||||
});
|
||||
}
|
||||
|
||||
async getUserById(user_id: number): Promise<FullDBUser | null> {
|
||||
export async function getUserById(
|
||||
user_id: number
|
||||
): Promise<FullDBUser | null> {
|
||||
return prisma_client.user.findFirstOrThrow({
|
||||
where: { id: user_id },
|
||||
...fullDBUser
|
||||
});
|
||||
}
|
||||
|
||||
async createUser(
|
||||
export async function createUser(
|
||||
supabase_uid: string,
|
||||
display_name: string,
|
||||
email: string
|
||||
@@ -65,7 +67,7 @@ export default class AuthService {
|
||||
});
|
||||
}
|
||||
|
||||
async deleteUser(user_id: number): Promise<FullDBUser> {
|
||||
export async function deleteUser(user_id: number): Promise<FullDBUser> {
|
||||
return prisma_client.user.delete({
|
||||
where: { id: user_id },
|
||||
...fullDBUser
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
import prisma_client from '~~/prisma/prisma.client';
|
||||
import { openai } from './openai.client';
|
||||
import { AccountLimitError } from './errors';
|
||||
import AccountService from './account.service';
|
||||
import { AccountService } from './account.service';
|
||||
|
||||
export default class NotesService {
|
||||
async getAllNotes() {
|
||||
export namespace NotesService {
|
||||
export async function getAllNotes() {
|
||||
return prisma_client.note.findMany();
|
||||
}
|
||||
|
||||
async getNoteById(id: number) {
|
||||
export async function getNoteById(id: number) {
|
||||
return prisma_client.note.findUniqueOrThrow({ where: { id } });
|
||||
}
|
||||
|
||||
async getNotesForAccountId(account_id: number) {
|
||||
export async function getNotesForAccountId(account_id: number) {
|
||||
return prisma_client.note.findMany({ where: { account_id } });
|
||||
}
|
||||
|
||||
async createNote(account_id: number, note_text: string) {
|
||||
export async function createNote(account_id: number, note_text: string) {
|
||||
const account = await prisma_client.account.findFirstOrThrow({
|
||||
where: { id: account_id },
|
||||
include: { notes: true }
|
||||
@@ -31,17 +31,19 @@ export default class NotesService {
|
||||
return prisma_client.note.create({ data: { account_id, note_text } });
|
||||
}
|
||||
|
||||
async updateNote(id: number, note_text: string) {
|
||||
export async function updateNote(id: number, note_text: string) {
|
||||
return prisma_client.note.update({ where: { id }, data: { note_text } });
|
||||
}
|
||||
|
||||
async deleteNote(id: number) {
|
||||
export async function deleteNote(id: number) {
|
||||
return prisma_client.note.delete({ where: { id } });
|
||||
}
|
||||
|
||||
async generateAINoteFromPrompt(userPrompt: string, account_id: number) {
|
||||
const accountService = new AccountService();
|
||||
const account = await accountService.checkAIGenCount(account_id);
|
||||
export async function generateAINoteFromPrompt(
|
||||
userPrompt: string,
|
||||
account_id: number
|
||||
) {
|
||||
const account = await AccountService.checkAIGenCount(account_id);
|
||||
|
||||
const prompt = `
|
||||
Write an interesting short note about ${userPrompt}.
|
||||
@@ -56,7 +58,7 @@ export default class NotesService {
|
||||
n: 1
|
||||
});
|
||||
|
||||
await accountService.incrementAIGenCount(account);
|
||||
await AccountService.incrementAIGenCount(account);
|
||||
|
||||
return completion.data.choices[0].text;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
export class UtilService {
|
||||
public static addMonths(date: Date, months: number): Date {
|
||||
export namespace UtilService {
|
||||
export function addMonths(date: Date, months: number): Date {
|
||||
const d = date.getDate();
|
||||
date.setMonth(date.getMonth() + +months);
|
||||
if (date.getDate() != d) {
|
||||
@@ -8,12 +8,12 @@ export class UtilService {
|
||||
return date;
|
||||
}
|
||||
|
||||
public static getErrorMessage(error: unknown) {
|
||||
export function getErrorMessage(error: unknown) {
|
||||
if (error instanceof Error) return error.message;
|
||||
return String(error);
|
||||
}
|
||||
|
||||
public static stringifySafely(obj: any) {
|
||||
export function stringifySafely(obj: any) {
|
||||
let cache: any[] = [];
|
||||
let str = JSON.stringify(obj, function (key, value) {
|
||||
if (typeof value === 'object' && value !== null) {
|
||||
|
||||
Reference in New Issue
Block a user