@@ -5,6 +5,7 @@ import { eq, and, asc, sql } from 'drizzle-orm';
55import type { Actions } from './$types' ;
66import { sendSlackDM } from '$lib/server/slack.js' ;
77import { getReviewHistory } from '../../getReviewHistory.server' ;
8+ import { getCurrentlyPrinting } from '../utils.server' ;
89
910export async function load ( { locals, params } ) {
1011 if ( ! locals . user ) {
@@ -79,10 +80,13 @@ export async function load({ locals, params }) {
7980 . where ( and ( eq ( devlog . projectId , queriedProject . project . id ) , eq ( devlog . deleted , false ) ) )
8081 . orderBy ( asc ( devlog . createdAt ) ) ;
8182
83+ const currentlyPrinting = await getCurrentlyPrinting ( locals . user ) ;
84+
8285 return {
8386 project : queriedProject ,
8487 devlogs,
85- reviews : await getReviewHistory ( id )
88+ reviews : await getReviewHistory ( id ) ,
89+ currentlyPrinting
8690 } ;
8791}
8892
@@ -95,8 +99,16 @@ export const actions = {
9599 throw error ( 403 , { message : 'oi get out' } ) ;
96100 }
97101
102+ const currentlyPrinting = await getCurrentlyPrinting ( locals . user ) ;
103+
98104 const id : number = parseInt ( params . id ) ;
99105
106+ if ( currentlyPrinting && currentlyPrinting . id !== id ) {
107+ return error ( 400 , {
108+ message : 'you are already printing something else right now'
109+ } ) ;
110+ }
111+
100112 const [ queriedProject ] = await db
101113 . select ( {
102114 id : project . id ,
@@ -124,8 +136,7 @@ export const actions = {
124136 . update ( project )
125137 . set ( {
126138 status : 'printing' ,
127- printedBy : locals . user . id ,
128- claimedAt : new Date ( )
139+ printedBy : locals . user . id
129140 } )
130141 . where ( eq ( project . id , id ) ) ;
131142
@@ -170,8 +181,7 @@ export const actions = {
170181 . update ( project )
171182 . set ( {
172183 status : 't1_approved' ,
173- printedBy : null ,
174- claimedAt : null
184+ printedBy : null
175185 } )
176186 . where ( eq ( project . id , id ) ) ;
177187
@@ -186,13 +196,20 @@ export const actions = {
186196 throw error ( 403 , { message : 'oi get out' } ) ;
187197 }
188198
199+ const currentlyPrinting = await getCurrentlyPrinting ( locals . user ) ;
200+
189201 const id : number = parseInt ( params . id ) ;
190202
203+ if ( ! currentlyPrinting || currentlyPrinting . id !== id ) {
204+ return error ( 400 , {
205+ message : "you can only print a project if you've marked it as you're printing it"
206+ } ) ;
207+ }
208+
191209 const [ queriedProject ] = await db
192210 . select ( {
193211 id : project . id ,
194- status : project . status ,
195- printedBy : project . printedBy
212+ status : project . status
196213 } )
197214 . from ( project )
198215 . where ( and ( eq ( project . id , id ) , eq ( project . deleted , false ) ) )
@@ -206,12 +223,6 @@ export const actions = {
206223 return error ( 403 , { message : 'project is not marked as currently printing' } ) ;
207224 }
208225
209- if ( queriedProject . printedBy !== locals . user . id ) {
210- return error ( 400 , {
211- message : "you can only print a project if you've marked it as you're printing it"
212- } ) ;
213- }
214-
215226 const data = await request . formData ( ) ;
216227 const filamentUsed = data . get ( 'filament' ) ;
217228 const notes = data . get ( 'notes' ) ?. toString ( ) ;
@@ -241,8 +252,7 @@ export const actions = {
241252 await db
242253 . update ( project )
243254 . set ( {
244- status : 'printed' ,
245- claimedAt : null
255+ status : 'printed'
246256 } )
247257 . where ( eq ( project . id , id ) ) ;
248258
0 commit comments