Skip to content

Commit

Permalink
feat(users): updating models and relationships for users. adding dele…
Browse files Browse the repository at this point in the history
…te account option to user management for admins
  • Loading branch information
tomgobich committed Sep 18, 2024
1 parent 683d095 commit ca1ee4b
Show file tree
Hide file tree
Showing 12 changed files with 489 additions and 16 deletions.
16 changes: 13 additions & 3 deletions app/Controllers/Http/PlansController.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import Plan from 'App/Models/Plan'
import Route from '@ioc:Adonis/Core/Route';
import CouponValidator from 'App/Validators/CouponValidator';
import Route from '@ioc:Adonis/Core/Route'
import CouponValidator from 'App/Validators/CouponValidator'
import Plans from 'App/Enums/Plans'
import StripeSubscriptionStatuses from 'App/Enums/StripeSubscriptionStatuses'

export default class PlansController {
public async index({ view, request, bouncer }: HttpContextContract) {
await bouncer.with('StudioPolicy').authorize('adminOnly')

const page = request.input('page', 1)
const plans = await Plan.query()
.withCount('users')
.withCount('users', (query) =>
query.where((query) => {
query
.where('planId', Plans.FOREVER)
.orWhere('planId', Plans.FREE)
.orWhere('stripeSubscriptionStatus', StripeSubscriptionStatuses.ACTIVE)
.orWhere('stripeSubscriptionStatus', StripeSubscriptionStatuses.COMPLETE)
})
)
.orderBy('createdAt', 'desc')
.paginate(page, 30)

Expand Down
25 changes: 24 additions & 1 deletion app/Controllers/Http/UsersController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,28 @@ export default class UsersController {
return response.redirect().toRoute('studio.users.show', { id: params.id })
}

public async destroy({}: HttpContextContract) {}
public async destroy({ response, params, session, bouncer }: HttpContextContract) {
await bouncer.with('StudioPolicy').authorize('adminOnly')

const user = await User.findOrFail(params.id)

if (user.stripeCustomerId) {
session.flash('error', 'Please ensure this user is not an active subscriber and dissociate their stripe account first')
return response.redirect().back()
}

const success = await UserService.destroy(user)

if (!success) {
session.flash(
'error',
'Apologies, but something went wrong.'
)
return response.redirect().back()
}

session.flash('success', 'User successfully deleted')

return response.redirect().toRoute('studio.users.index')
}
}
13 changes: 13 additions & 0 deletions app/Enums/StripeSubscriptionStatuses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
enum StripeSubscriptionStatuses {
COMPLETE = 'complete',
ACTIVE = 'active',
PAST_DUE = 'past_due',
UNPAID = 'unpaid',
CANCELED = 'canceled',
INCOMPLETE = 'incomplete',
INCOMPLETE_EXPIRED = 'incomplete_expired',
TRIALING = 'trialing',
PAUSED = 'paused'
}

export default StripeSubscriptionStatuses
83 changes: 83 additions & 0 deletions app/Models/Discussion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { DateTime } from 'luxon'
import { BelongsTo, belongsTo, column, computed, hasMany, HasMany, manyToMany, ManyToMany } from '@ioc:Adonis/Lucid/Orm'
import AppBaseModel from 'App/Models/AppBaseModel'
import DiscussionViewTypes from 'App/Enums/DiscussionViewTypes'
import User from './User'
import DiscussionView from './DiscussionView'
import Taxonomy from './Taxonomy'
import Comment from './Comment'

export default class Discussion extends AppBaseModel {
@column({ isPrimary: true })
declare id: number

@column()
declare userId: number

@column()
declare taxonomyId: number

@column()
declare stateId: number

@column()
declare title: string

@column()
declare slug: string

@column()
declare body: string

@column.dateTime({ autoCreate: true })
declare createdAt: DateTime

@column.dateTime({ autoCreate: true, autoUpdate: true })
declare updatedAt: DateTime

@computed()
get createdAtDisplay() {
if (!this.createdAt) return ''

if (DateTime.now().year === this.createdAt.year) {
return this.createdAt.toFormat('MMM dd')
}

return this.createdAt.toFormat('MMM dd, yy')
}

@computed()
get updatedAtDisplay() {
if (!this.updatedAt) return ''

if (DateTime.now().year === this.updatedAt.year) {
return this.updatedAt.toFormat('MMM dd')
}

return this.updatedAt.toFormat('MMM dd, yy')
}

@belongsTo(() => User)
declare user: BelongsTo<typeof User>

@belongsTo(() => Taxonomy)
declare taxonomy: BelongsTo<typeof Taxonomy>

@hasMany(() => Comment)
declare comments: HasMany<typeof Comment>

@hasMany(() => DiscussionView, {
onQuery(query) {
query.where('typeId', DiscussionViewTypes.VIEW)
}
})
declare views: HasMany<typeof DiscussionView>

@hasMany(() => DiscussionView)
declare impressions: HasMany<typeof DiscussionView>

@manyToMany(() => User, {
pivotTable: 'discussion_votes'
})
declare votes: ManyToMany<typeof User>
}
38 changes: 38 additions & 0 deletions app/Models/DiscussionView.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { DateTime } from 'luxon'
import { BelongsTo, belongsTo, column } from '@ioc:Adonis/Lucid/Orm'
import AppBaseModel from 'App/Models/AppBaseModel'
import DiscussionViewTypes from 'App/Enums/DiscussionViewTypes'
import User from './User'
import Discussion from './Discussion'

export default class DiscussionView extends AppBaseModel {
@column({ isPrimary: true })
declare id: number

@column()
declare userId: number | null

@column()
declare discussionId: number

@column()
declare typeId: DiscussionViewTypes

@column()
declare ipAddress: string

@column()
declare userAgent: string

@column.dateTime({ autoCreate: true })
declare createdAt: DateTime

@column.dateTime({ autoCreate: true, autoUpdate: true })
declare updatedAt: DateTime

@belongsTo(() => User)
declare user: BelongsTo<typeof User>

@belongsTo(() => Discussion)
declare discussion: BelongsTo<typeof Discussion>
}
27 changes: 27 additions & 0 deletions app/Models/DiscussionVote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { BaseModel, column, belongsTo, BelongsTo } from "@ioc:Adonis/Lucid/Orm"
import { DateTime } from "luxon"
import Discussion from "./Discussion"
import User from "./User"

export default class DiscussionVote extends BaseModel {
@column({ isPrimary: true })
declare id: number

@column()
declare userId: number | null

@column()
declare discussionId: number | null

@column.dateTime({ autoCreate: true })
declare createdAt: DateTime

@column.dateTime({ autoCreate: true, autoUpdate: true })
declare updatedAt: DateTime

@belongsTo(() => User)
declare user: BelongsTo<typeof User>

@belongsTo(() => Discussion)
declare discussion: BelongsTo<typeof Discussion>
}
43 changes: 43 additions & 0 deletions app/Models/Progress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { DateTime } from 'luxon'
import { BelongsTo, belongsTo, column } from '@ioc:Adonis/Lucid/Orm'
import AppBaseModel from 'App/Models/AppBaseModel'
import Post from 'App/Models/Post'
import Collection from 'App/Models/Collection'

export default class Progress extends AppBaseModel {
@column({ isPrimary: true })
declare id: number

@column()
declare userId: number

@column()
declare postId: number | null

@column()
declare collectionId: number | null

@column()
declare readPercent: number | null

@column()
declare watchPercent: number | null

@column()
declare watchSeconds: number

@column()
declare isCompleted: boolean

@column.dateTime({ autoCreate: true })
declare createdAt: DateTime

@column.dateTime({ autoCreate: true, autoUpdate: true })
declare updatedAt: DateTime

@belongsTo(() => Post)
declare post: BelongsTo<typeof Post>

@belongsTo(() => Collection)
declare collection: BelongsTo<typeof Collection>
}
83 changes: 83 additions & 0 deletions app/Models/SessionLog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { DateTime } from 'luxon'
import { BelongsTo, belongsTo, column } from '@ioc:Adonis/Lucid/Orm'
import AppBaseModel from 'App/Models/AppBaseModel'
import User from './User'

export default class SessionLog extends AppBaseModel {
@column({ isPrimary: true })
declare id: number

@column()
declare userId: number

@column()
declare token: string

@column()
declare ipAddress: string | null

@column()
declare userAgent: string | null

@column()
declare browserName: string | null

@column()
declare browserEngine: string | null

@column()
declare browserVersion: string | null

@column()
declare deviceModel: string | null

@column()
declare deviceType: string | null

@column()
declare deviceVendor: string | null

@column()
declare osName: string | null

@column()
declare osVersion: string | null

@column()
declare city: string | null

@column()
declare country: string | null

@column()
declare countryCode: string | null

@column.dateTime()
declare lastTouchedAt: DateTime | null

@column.dateTime()
declare loginAt: DateTime | null

@column()
declare loginSuccessful: boolean

@column.dateTime()
declare logoutAt: DateTime | null

@column()
declare forceLogout: boolean

@column()
declare isRememberSession: boolean

@column.dateTime({ autoCreate: true })
declare createdAt: DateTime

@column.dateTime({ autoCreate: true, autoUpdate: true })
declare updatedAt: DateTime

@belongsTo(() => User)
declare user: BelongsTo<typeof User>

isCurrentSession: boolean = false
}
Loading

0 comments on commit ca1ee4b

Please sign in to comment.