@@ -2,14 +2,15 @@ import { z } from 'zod'
22
33import { CustomFieldPosition , CustomFieldType , Prisma } from '@prisma/client'
44import prisma from '../../prisma.js'
5- import { definePublicQueryProcedure } from '../../types/defineProcedure.js'
5+ import { defineProtectedQueryProcedure , definePublicQueryProcedure } from '../../types/defineProcedure.js'
66import {
77 calculatePagination ,
88 defineEmptyQueryResponse ,
99 defineQueryResponse ,
1010 defineTableInput ,
1111} from '../../types/defineTableProcedure.js'
1212import { boolish } from '../../util/zod.js'
13+ import { getGliederungRequireAdmin } from '../../util/getGliederungRequireAdmin.js'
1314
1415const baseFilter = z . strictObject ( {
1516 entity : z . enum ( [ 'veranstaltung' , 'unterveranstaltung' ] ) ,
@@ -23,6 +24,7 @@ export const customFieldsTable = definePublicQueryProcedure({
2324 async handler ( { input : { entity, entityId, position } } ) {
2425 if ( entity === 'veranstaltung' ) {
2526 return await prisma . customField . findMany ( {
27+ orderBy : [ { order : { sort : 'asc' , nulls : 'last' } } ] ,
2628 where : {
2729 veranstaltungId : entityId ,
2830 positions :
@@ -35,6 +37,14 @@ export const customFieldsTable = definePublicQueryProcedure({
3537 } )
3638 } else if ( entity === 'unterveranstaltung' ) {
3739 return await prisma . customField . findMany ( {
40+ orderBy : [
41+ {
42+ unterveranstaltungId : { sort : 'asc' , nulls : 'first' } ,
43+ } ,
44+ {
45+ order : { sort : 'asc' , nulls : 'last' } ,
46+ } ,
47+ ] ,
3848 where : {
3949 positions :
4050 position === undefined
@@ -64,8 +74,9 @@ export const customFieldsTable = definePublicQueryProcedure({
6474 } ,
6575} )
6676
67- export const customFieldsList = definePublicQueryProcedure ( {
77+ export const customFieldsList = defineProtectedQueryProcedure ( {
6878 key : 'table' ,
79+ roleIds : [ 'ADMIN' , 'GLIEDERUNG_ADMIN' ] ,
6980 inputSchema : baseFilter . extend ( {
7081 table : defineTableInput ( {
7182 filter : {
@@ -74,10 +85,14 @@ export const customFieldsList = definePublicQueryProcedure({
7485 required : boolish ,
7586 position : z . nativeEnum ( CustomFieldPosition ) ,
7687 } ,
77- orderBy : [ 'name' ] ,
88+ orderBy : [ 'name' , 'order' ] ,
7889 } ) ,
7990 } ) ,
80- async handler ( { input } ) {
91+ async handler ( { ctx, input } ) {
92+ if ( ctx . account . role === 'GLIEDERUNG_ADMIN' ) {
93+ await getGliederungRequireAdmin ( ctx . accountId )
94+ }
95+
8196 const where : Prisma . CustomFieldWhereInput = {
8297 name : {
8398 contains : input . table ?. filter ?. name ,
@@ -93,22 +108,39 @@ export const customFieldsList = definePublicQueryProcedure({
93108 } ,
94109 }
95110
111+ if ( input . entity === 'veranstaltung' ) {
112+ where . veranstaltungId = input . entityId
113+ } else if ( input . entity === 'unterveranstaltung' ) {
114+ where . OR = [
115+ {
116+ unterveranstaltungId : input . entityId ,
117+ } ,
118+ {
119+ veranstaltung : {
120+ unterveranstaltungen : {
121+ some : {
122+ id : input . entityId ,
123+ } ,
124+ } ,
125+ } ,
126+ } ,
127+ ]
128+ }
129+
96130 const total = await prisma . customField . count ( { where } )
97131 const { pageIndex, pageSize, pages } = calculatePagination ( total , input . table ?. pagination )
98132
99133 if ( input . entity === 'veranstaltung' ) {
100134 const customFields = await prisma . customField . findMany ( {
101135 take : pageSize ,
102136 skip : pageSize * pageIndex ,
103- where : {
104- ...where ,
105- veranstaltungId : input . entityId ,
106- } ,
137+ where,
107138 orderBy : input . table ?. orderBy ,
108139 select : {
109140 id : true ,
110141 name : true ,
111142 description : true ,
143+ order : true ,
112144 type : true ,
113145 positions : true ,
114146 required : true ,
@@ -122,27 +154,18 @@ export const customFieldsList = definePublicQueryProcedure({
122154 const customFields = await prisma . customField . findMany ( {
123155 take : pageSize ,
124156 skip : pageSize * pageIndex ,
125- where : {
126- ...where ,
127- OR : [
128- {
129- unterveranstaltungId : input . entityId ,
130- } ,
131- {
132- veranstaltung : {
133- unterveranstaltungen : {
134- some : {
135- id : input . entityId ,
136- } ,
137- } ,
138- } ,
139- } ,
140- ] ,
141- } ,
157+ orderBy : [
158+ {
159+ unterveranstaltungId : { sort : 'asc' , nulls : 'first' } ,
160+ } ,
161+ ...( input . table ?. orderBy ?? [ ] ) ,
162+ ] ,
163+ where,
142164 select : {
143165 id : true ,
144166 name : true ,
145167 description : true ,
168+ order : true ,
146169 type : true ,
147170 positions : true ,
148171 required : true ,
0 commit comments