Skip to content

Commit a38bc1f

Browse files
chore: wip
1 parent afc2785 commit a38bc1f

File tree

19 files changed

+79
-39
lines changed

19 files changed

+79
-39
lines changed

app/Actions/Payment/CreateInvoiceSubscription.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default new Action({
99
const user = await User.find(1)
1010

1111
const subscription = await user?.newSubscriptionInvoice('stacks_pro_yearly', {
12-
allowPromotions: true, // Example option for allowing promotions
12+
allowPromotions: true,
1313
})
1414

1515
return subscription

app/Models/User.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export default {
4343
},
4444

4545
hasOne: ['Post', 'Subscriber'],
46-
hasMany: ['Deployment'],
46+
hasMany: ['Deployment', 'Subscription'],
4747

4848
belongsToMany: ['Team'],
4949

storage/framework/core/components/modal/src/components/Transitions.vue

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ function handleClose() {
1313
}
1414
1515
const renderedCode = computed(() => {
16-
17-
1816
if (currentTransition.value === 'custom') {
1917
return `<Modal :visible="visible" @close="handleClose" transition="custom">
2018
<template #closeButton />

storage/framework/core/components/notification/src/components/Expand.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ async function handleCopyCode() {
6363
Default
6464
</button>
6565
</div>
66-
<div class="code-block group relative">
66+
<div class="group code-block relative">
6767
<Highlight
6868
language="javascript"
6969
class-name="rounded-md text-xs"

storage/framework/core/components/notification/src/components/Usage.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async function handleCopyCode() {
3333
<p class="my-3 text-base">
3434
Render the toaster in the root of your app.
3535
</p>
36-
<div class="group code-block relative">
36+
<div class="code-block group relative">
3737
<Highlight
3838
class-name="rounded-md text-xs"
3939
language="xml"

storage/framework/core/orm/src/utils.ts

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,19 @@ export async function getRelations(model: Model, modelName: string): Promise<Rel
6666
if (hasRelations(model, relation)) {
6767
for (const relationInstance of (model[relation as keyof Model] as any[]) || []) {
6868
let relationModel = relationInstance.model
69-
69+
let modelRelation: Model
7070
if (isString(relationInstance)) {
7171
relationModel = relationInstance
7272
}
7373

7474
const modelRelationPath = path.userModelsPath(`${relationModel}.ts`)
75-
const modelRelation = (await import(modelRelationPath)).default as Model
75+
const coreModelRelationPath = path.storagePath(`framework/database/models/generated/${relationModel}.ts`)
76+
77+
if (fs.existsSync(modelRelationPath))
78+
modelRelation = (await import(modelRelationPath)).default as Model
79+
else
80+
modelRelation = (await import(coreModelRelationPath)).default as Model
81+
7682
const modelRelationTable = getTableName(modelRelation, modelRelationPath)
7783
const formattedModelName = modelName.toLowerCase()
7884

@@ -165,8 +171,9 @@ export async function getPivotTables(
165171
return []
166172
}
167173

168-
export async function fetchOtherModelRelations(model: Model, modelName?: string): Promise<RelationConfig[]> {
174+
export async function fetchOtherModelRelations(modelName?: string): Promise<RelationConfig[]> {
169175
const modelFiles = globSync([path.userModelsPath('*.ts')], { absolute: true })
176+
170177
const modelRelations = []
171178

172179
for (let i = 0; i < modelFiles.length; i++) {
@@ -224,6 +231,7 @@ export function getFillableAttributes(attributes: Attributes | undefined): strin
224231

225232
export async function writeModelNames(): Promise<void> {
226233
const models = globSync([path.userModelsPath('*.ts')], { absolute: true })
234+
const coreModelFiles = globSync([path.storagePath('framework/database/models/generated/*.ts')], { absolute: true })
227235
let fileString = `export type ModelNames = `
228236

229237
for (let i = 0; i < models.length; i++) {
@@ -236,15 +244,30 @@ export async function writeModelNames(): Promise<void> {
236244
if (i < models.length - 1) {
237245
fileString += ' | '
238246
}
247+
}
248+
249+
fileString += ' | '
239250

240-
// Ensure the directory exists
241-
const typesDir = path.dirname(path.typesPath(`src/model-names.ts`))
242-
await fs.promises.mkdir(typesDir, { recursive: true })
251+
for (let j = 0; j < coreModelFiles.length; j++) {
252+
const modelPath = coreModelFiles[j] as string
243253

244-
// Write to the file
245-
const typeFilePath = path.typesPath(`src/model-names.ts`)
246-
await fs.promises.writeFile(typeFilePath, fileString, 'utf8')
254+
const model = (await import(modelPath)).default as Model
255+
const modelName = getModelName(model, modelPath)
256+
257+
fileString += `'${modelName}'`
258+
259+
if (j < coreModelFiles.length - 1) {
260+
fileString += ' | '
261+
}
247262
}
263+
264+
// Ensure the directory exists
265+
const typesDir = path.dirname(path.typesPath(`src/model-names.ts`))
266+
await fs.promises.mkdir(typesDir, { recursive: true })
267+
268+
// Write to the file
269+
const typeFilePath = path.typesPath(`src/model-names.ts`)
270+
await fs.promises.writeFile(typeFilePath, fileString, 'utf8')
248271
}
249272

250273
export async function writeModelRequest(): Promise<void> {
@@ -316,7 +339,8 @@ export async function writeModelRequest(): Promise<void> {
316339
fieldStringType += ` get(key: ${concatenatedFields}): ${entity}\n`
317340
}
318341

319-
const otherModelRelations = await fetchOtherModelRelations(model, modelName)
342+
const otherModelRelations = await fetchOtherModelRelations(modelName)
343+
320344
for (const otherModel of otherModelRelations) {
321345
fieldString += ` ${otherModel.foreignKey}: number\n `
322346
fieldStringType += ` get(key: '${otherModel.foreignKey}'): string \n`
@@ -1316,21 +1340,21 @@ export async function generateModelString(
13161340

13171341
jsonFields += '}'
13181342

1319-
const otherModelRelations = await fetchOtherModelRelations(model, modelName)
1343+
const otherModelRelations = await fetchOtherModelRelations(modelName)
13201344

13211345
for (const otherModelRelation of otherModelRelations) {
13221346
fieldString += ` ${otherModelRelation.foreignKey}?: number \n`
13231347
declareFields += `public ${otherModelRelation.foreignKey}: number | undefined \n `
13241348
constructorFields += `this.${otherModelRelation.foreignKey} = ${formattedModelName}?.${otherModelRelation.foreignKey}\n `
13251349
}
13261350

1327-
if (useTwoFactor)
1351+
if (useTwoFactor && tableName === 'users')
13281352
fieldString += 'two_factor_secret?: string \n'
13291353

1330-
if (usePasskey)
1354+
if (usePasskey && tableName === 'users')
13311355
fieldString += 'public_passkey?: string \n'
13321356

1333-
if (useBillable)
1357+
if (useBillable && tableName === 'users')
13341358
fieldString += 'stripe_id?: string \n'
13351359

13361360
if (useTimestamps) {
@@ -1490,7 +1514,7 @@ export async function generateModelString(
14901514
}
14911515
14921516
// Method to get a User by criteria
1493-
static async get(): Promise<UserModel[]> {
1517+
static async get(): Promise<${modelName}Model[]> {
14941518
const instance = new ${modelName}Model(null)
14951519
14961520
if (instance.hasSelect) {

storage/framework/core/payments/src/billable/subscription.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type Stripe from 'stripe'
2+
import type { SubscriptionModel } from '../../../../orm/src/models/Subscription'
23
import type { UserModel } from '../../../../orm/src/models/User'
3-
import { stripe } from '..'
44

5+
import { stripe } from '..'
56
import Subscription from '../../../../orm/src/models/Subscription'
67

78
export interface SubscriptionManager {
@@ -33,20 +34,21 @@ export const manageSubscription: SubscriptionManager = (() => {
3334
return subscription
3435
}
3536

36-
async function storeSubscription(options: Stripe.Subscription): Promise<any> {
37+
async function storeSubscription(options: Stripe.Subscription): Promise<SubscriptionModel> {
3738
const data = {
3839
user_id: 1,
39-
type: 'subscription',
40+
type: 'premium',
4041
stripe_id: options.id,
4142
stripe_status: options.status,
4243
stripe_price: options.items.data[0].price.id,
4344
quantity: options.items.data[0].quantity,
44-
trial_ends_at: options.trial_end,
45-
ends_at: options.cancel_at,
46-
last_used_at: options.current_period_end,
45+
trial_ends_at: String(options.trial_end),
46+
last_used_at: String(options.current_period_end),
4747
}
4848

49-
const subscriptionModel = Subscription.create(data)
49+
const subscriptionModel = await Subscription.create(data)
50+
51+
return subscriptionModel
5052
}
5153

5254
return { create }
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export type ModelNames = 'Project' | 'SubscriberEmail' | 'AccessToken' | 'Team' | 'Subscriber' | 'Deployment' | 'Release' | 'User' | 'Post'
1+
export type ModelNames = 'Project' | 'SubscriberEmail' | 'AccessToken' | 'Team' | 'Subscriber' | 'Deployment' | 'Release' | 'User' | 'Post' | 'Subscription' | 'Error'

storage/framework/orm/src/models/AccessToken.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export class AccessTokenModel {
166166
}
167167

168168
// Method to get a User by criteria
169-
static async get(): Promise<UserModel[]> {
169+
static async get(): Promise<AccessTokenModel[]> {
170170
const instance = new AccessTokenModel(null)
171171

172172
if (instance.hasSelect) {

storage/framework/orm/src/models/Deployment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export class DeploymentModel {
175175
}
176176

177177
// Method to get a User by criteria
178-
static async get(): Promise<UserModel[]> {
178+
static async get(): Promise<DeploymentModel[]> {
179179
const instance = new DeploymentModel(null)
180180

181181
if (instance.hasSelect) {

0 commit comments

Comments
 (0)