refactor(server): 规范化 licence 持久化结构
This commit is contained in:
@@ -4,7 +4,10 @@ import { generatedFields } from '../fields'
|
||||
export const uxConfigTable = sqliteTable('ux_config', {
|
||||
...generatedFields,
|
||||
singletonKey: text('singleton_key').notNull().unique().default('default'),
|
||||
licence: text('licence'),
|
||||
licencePayload: text('licence_payload'),
|
||||
licenceSignature: text('licence_signature'),
|
||||
licenceId: text('licence_id'),
|
||||
licenceExpireTime: text('licence_expire_time'),
|
||||
fingerprint: text('fingerprint').notNull(),
|
||||
platformPublicKey: text('platform_public_key'),
|
||||
pgpPrivateKey: text('pgp_private_key'),
|
||||
|
||||
@@ -32,17 +32,37 @@ export const ensureUxConfig = async (db: DB) => {
|
||||
.values({
|
||||
singletonKey: UX_CONFIG_KEY,
|
||||
fingerprint,
|
||||
licence: null,
|
||||
licencePayload: null,
|
||||
licenceSignature: null,
|
||||
licenceId: null,
|
||||
licenceExpireTime: null,
|
||||
})
|
||||
.returning()
|
||||
|
||||
return rows[0] as (typeof rows)[number]
|
||||
}
|
||||
|
||||
export const setUxLicence = async (db: DB, licence: string) => {
|
||||
export const setUxLicence = async (
|
||||
db: DB,
|
||||
licence: {
|
||||
payload: string
|
||||
signature: string
|
||||
licenceId: string
|
||||
expireTime: string
|
||||
},
|
||||
) => {
|
||||
const config = await ensureUxConfig(db)
|
||||
|
||||
const rows = await db.update(uxConfigTable).set({ licence }).where(eq(uxConfigTable.id, config.id)).returning()
|
||||
const rows = await db
|
||||
.update(uxConfigTable)
|
||||
.set({
|
||||
licencePayload: licence.payload,
|
||||
licenceSignature: licence.signature,
|
||||
licenceId: licence.licenceId,
|
||||
licenceExpireTime: licence.expireTime,
|
||||
})
|
||||
.where(eq(uxConfigTable.id, config.id))
|
||||
.returning()
|
||||
|
||||
return rows[0] as (typeof rows)[number]
|
||||
}
|
||||
@@ -57,10 +77,21 @@ export const setUxPgpPrivateKey = async (db: DB, pgpPrivateKey: string) => {
|
||||
|
||||
export const setUxPlatformPublicKey = async (db: DB, platformPublicKey: string) => {
|
||||
const config = await ensureUxConfig(db)
|
||||
const shouldClearLicence = config.platformPublicKey !== platformPublicKey
|
||||
|
||||
const rows = await db
|
||||
.update(uxConfigTable)
|
||||
.set({ platformPublicKey })
|
||||
.set({
|
||||
platformPublicKey,
|
||||
...(shouldClearLicence
|
||||
? {
|
||||
licencePayload: null,
|
||||
licenceSignature: null,
|
||||
licenceId: null,
|
||||
licenceExpireTime: null,
|
||||
}
|
||||
: {}),
|
||||
})
|
||||
.where(eq(uxConfigTable.id, config.id))
|
||||
.returning()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user