- Replaced Sequelize models with Drizzle ORM for app and app list management. - Updated routes in app-manager to utilize new database queries. - Removed obsolete Sequelize model files for app, app list, and app domain. - Introduced new helper functions for app and app domain management. - Enhanced user app management with improved file handling and user migration. - Adjusted public API routes to align with new database structure. - Implemented caching mechanisms for domain management.
103 lines
3.0 KiB
TypeScript
103 lines
3.0 KiB
TypeScript
import { app, db, schema } from '@/app.ts';
|
|
import { App, AppData } from '../module/app-drizzle.ts';
|
|
import { AppDomain, AppDomainHelper } from '../module/app-domain-drizzle.ts';
|
|
import { eq, and } from 'drizzle-orm';
|
|
import { randomUUID } from 'crypto';
|
|
|
|
app
|
|
.route({
|
|
path: 'app',
|
|
key: 'getDomainApp',
|
|
})
|
|
.define(async (ctx) => {
|
|
const { domain } = ctx.query.data;
|
|
const domainInfos = await db.select().from(schema.kvAppDomain).where(eq(schema.kvAppDomain.domain, domain)).limit(1);
|
|
const domainInfo = domainInfos[0];
|
|
if (!domainInfo || !domainInfo.appId) {
|
|
ctx.throw(404, 'app not found');
|
|
}
|
|
const apps = await db.select().from(schema.kvApp).where(eq(schema.kvApp.id, domainInfo.appId)).limit(1);
|
|
const appFound = apps[0];
|
|
if (!appFound) {
|
|
ctx.throw(404, 'app not found');
|
|
}
|
|
ctx.body = appFound;
|
|
return ctx;
|
|
})
|
|
.addTo(app);
|
|
|
|
app
|
|
.route({
|
|
path: 'app-domain',
|
|
key: 'create',
|
|
middleware: ['auth'],
|
|
})
|
|
.define(async (ctx) => {
|
|
const tokenUser = ctx.state.tokenUser;
|
|
const uid = tokenUser.uid;
|
|
const { domain, appId } = ctx.query.data || {};
|
|
if (!domain || !appId) {
|
|
ctx.throw(400, 'domain and appId are required');
|
|
}
|
|
const newDomains = await db.insert(schema.kvAppDomain).values({ id: randomUUID(), domain, appId, uid }).returning();
|
|
const domainInfo = newDomains[0];
|
|
ctx.body = domainInfo;
|
|
return ctx;
|
|
})
|
|
.addTo(app);
|
|
|
|
app
|
|
.route({
|
|
path: 'app-domain',
|
|
key: 'update',
|
|
middleware: ['auth'],
|
|
})
|
|
.define(async (ctx) => {
|
|
const tokenUser = ctx.state.tokenUser;
|
|
const uid = tokenUser.uid;
|
|
const { id, domain, appId, status } = ctx.query.data || {};
|
|
if (!domain && !id) {
|
|
ctx.throw(400, 'domain and id are required at least one');
|
|
}
|
|
if (!status) {
|
|
ctx.throw(400, 'status is required');
|
|
}
|
|
let domainInfo: AppDomain | undefined;
|
|
if (id) {
|
|
const domains = await db.select().from(schema.kvAppDomain).where(eq(schema.kvAppDomain.id, id)).limit(1);
|
|
domainInfo = domains[0];
|
|
}
|
|
if (!domainInfo && domain) {
|
|
const domains = await db.select().from(schema.kvAppDomain).where(and(
|
|
eq(schema.kvAppDomain.domain, domain),
|
|
eq(schema.kvAppDomain.appId, appId)
|
|
)).limit(1);
|
|
domainInfo = domains[0];
|
|
}
|
|
if (!domainInfo) {
|
|
ctx.throw(404, 'domain not found');
|
|
}
|
|
if (domainInfo.uid !== uid) {
|
|
ctx.throw(403, 'domain must be owned by the user');
|
|
}
|
|
if (!AppDomainHelper.checkCanUpdateStatus(domainInfo.status!, status)) {
|
|
ctx.throw(400, 'domain status can not be updated');
|
|
}
|
|
const updateData: any = {};
|
|
if (status) {
|
|
updateData.status = status;
|
|
}
|
|
if (appId) {
|
|
updateData.appId = appId;
|
|
}
|
|
updateData.updatedAt = new Date().toISOString();
|
|
const updateResult = await db.update(schema.kvAppDomain)
|
|
.set(updateData)
|
|
.where(eq(schema.kvAppDomain.id, domainInfo.id))
|
|
.returning();
|
|
const updatedDomain = updateResult[0];
|
|
ctx.body = updatedDomain;
|
|
return ctx;
|
|
})
|
|
.addTo(app);
|