import type { HttpContext } from '@adonisjs/core/http' import Computer from '#models/computer' import { updateShareSettingsValidator } from '#validators/Computers/share' import { disableSharing, enableSharing, rotateShareToken } from '#services/computer_share_service' export default class ComputerShareController { /** * Update the share settings (enable/disable + which prices are visible) * for a computer owned by the current user. */ async update({ auth, params, request, response }: HttpContext) { const user = await auth.authenticate() const computer = await Computer.query() .where('user_id', user.id) .andWhere('id', params.id) .firstOrFail() const { enabled, showSellPrice, showCost } = await request.validateUsing( updateShareSettingsValidator ) if (enabled) { await enableSharing(computer, { showSellPrice, showCost }) } else { await disableSharing(computer) } return response.redirect(`/computers/${computer.id}`) } /** * Rotate the share token for a computer, invalidating any previously * distributed public link. */ async rotate({ auth, params, response }: HttpContext) { const user = await auth.authenticate() const computer = await Computer.query() .where('user_id', user.id) .andWhere('id', params.id) .firstOrFail() await rotateShareToken(computer) return response.redirect(`/computers/${computer.id}`) } }