get to prod tasks
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { eq, and, desc, count, gte } from "drizzle-orm";
|
||||
import { eq, and, desc, count, gte, inArray } from "drizzle-orm";
|
||||
import { db } from "~/server/db";
|
||||
import {
|
||||
subscriptions,
|
||||
@@ -21,21 +21,42 @@ import {
|
||||
parseAddress,
|
||||
getLastSnapshot,
|
||||
} from "./hometitle/scanner";
|
||||
import {
|
||||
getEffectiveTier,
|
||||
getActiveTrials,
|
||||
hasFeatureAccess,
|
||||
type SubWithEffectiveTier,
|
||||
} from "~/server/lib/tier";
|
||||
|
||||
async function getSubscription(userId: string) {
|
||||
async function getSubscription(userId: string): Promise<SubWithEffectiveTier> {
|
||||
const [sub] = await db
|
||||
.select()
|
||||
.from(subscriptions)
|
||||
.where(and(eq(subscriptions.userId, userId), eq(subscriptions.status, "active")))
|
||||
.where(and(
|
||||
eq(subscriptions.userId, userId),
|
||||
inArray(subscriptions.status, ["active", "trialing"]),
|
||||
))
|
||||
.limit(1);
|
||||
if (!sub) {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: "No active subscription found" });
|
||||
}
|
||||
return sub;
|
||||
const trials = await getActiveTrials(userId);
|
||||
return {
|
||||
id: sub.id,
|
||||
userId: sub.userId,
|
||||
tier: sub.tier as SubWithEffectiveTier["tier"],
|
||||
status: sub.status as SubWithEffectiveTier["status"],
|
||||
effectiveTier: getEffectiveTier(sub.tier as SubWithEffectiveTier["tier"], sub.status as SubWithEffectiveTier["status"]),
|
||||
isTrialing: sub.status === "trialing",
|
||||
trials,
|
||||
};
|
||||
}
|
||||
|
||||
export async function getProperties(userId: string) {
|
||||
const sub = await getSubscription(userId);
|
||||
if (!hasFeatureAccess(sub, "hometitle")) {
|
||||
throw new TRPCError({ code: "FORBIDDEN", message: "HomeTitle requires a Plus subscription or active feature trial" });
|
||||
}
|
||||
const items = await db
|
||||
.select()
|
||||
.from(propertyWatchlistItems)
|
||||
@@ -56,6 +77,9 @@ export async function addProperty(
|
||||
ownerName?: string,
|
||||
) {
|
||||
const sub = await getSubscription(userId);
|
||||
if (!hasFeatureAccess(sub, "hometitle")) {
|
||||
throw new TRPCError({ code: "FORBIDDEN", message: "HomeTitle requires a Plus subscription or active feature trial" });
|
||||
}
|
||||
|
||||
const parsed = parseAddress(address);
|
||||
const coords = await geocodeAddress(address);
|
||||
@@ -94,6 +118,9 @@ export async function addProperty(
|
||||
|
||||
export async function removeProperty(userId: string, propertyId: string) {
|
||||
const sub = await getSubscription(userId);
|
||||
if (!hasFeatureAccess(sub, "hometitle")) {
|
||||
throw new TRPCError({ code: "FORBIDDEN", message: "HomeTitle requires a Plus subscription or active feature trial" });
|
||||
}
|
||||
|
||||
const [item] = await db
|
||||
.select()
|
||||
@@ -121,6 +148,9 @@ export async function removeProperty(userId: string, propertyId: string) {
|
||||
|
||||
export async function getSnapshots(userId: string, propertyId: string) {
|
||||
const sub = await getSubscription(userId);
|
||||
if (!hasFeatureAccess(sub, "hometitle")) {
|
||||
throw new TRPCError({ code: "FORBIDDEN", message: "HomeTitle requires a Plus subscription or active feature trial" });
|
||||
}
|
||||
|
||||
const [item] = await db
|
||||
.select()
|
||||
@@ -152,6 +182,9 @@ export async function getChanges(
|
||||
filters?: { severity?: string; changeType?: string },
|
||||
) {
|
||||
const sub = await getSubscription(userId);
|
||||
if (!hasFeatureAccess(sub, "hometitle")) {
|
||||
throw new TRPCError({ code: "FORBIDDEN", message: "HomeTitle requires a Plus subscription or active feature trial" });
|
||||
}
|
||||
|
||||
const [item] = await db
|
||||
.select()
|
||||
@@ -202,6 +235,9 @@ export async function getChanges(
|
||||
|
||||
export async function getAlerts(userId: string) {
|
||||
const sub = await getSubscription(userId);
|
||||
if (!hasFeatureAccess(sub, "hometitle")) {
|
||||
throw new TRPCError({ code: "FORBIDDEN", message: "HomeTitle requires a Plus subscription or active feature trial" });
|
||||
}
|
||||
|
||||
const items = await db
|
||||
.select()
|
||||
@@ -248,7 +284,7 @@ export async function getAlerts(userId: string) {
|
||||
|
||||
async function checkTierLimits(userId: string): Promise<{ allowed: boolean; reason?: string }> {
|
||||
const sub = await getSubscription(userId);
|
||||
const tier = sub.tier;
|
||||
const tier = sub.effectiveTier;
|
||||
|
||||
if (tier === "premium") {
|
||||
return { allowed: true };
|
||||
@@ -289,6 +325,10 @@ async function checkTierLimits(userId: string): Promise<{ allowed: boolean; reas
|
||||
export async function runScan(userId: string): Promise<{ scanId: string }> {
|
||||
const sub = await getSubscription(userId);
|
||||
|
||||
if (!hasFeatureAccess(sub, "hometitle")) {
|
||||
throw new TRPCError({ code: "FORBIDDEN", message: "HomeTitle requires a Plus subscription or active feature trial" });
|
||||
}
|
||||
|
||||
const tierCheck = await checkTierLimits(userId);
|
||||
if (!tierCheck.allowed) {
|
||||
throw new TRPCError({ code: "TOO_MANY_REQUESTS", message: tierCheck.reason });
|
||||
|
||||
Reference in New Issue
Block a user