11/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
22
33import { count , eq } from "@dariah-eric/database" ;
4- import { db , type Transaction } from "@dariah-eric/database/client" ;
4+ import { db } from "@dariah-eric/database/client" ;
55import * as schema from "@dariah-eric/database/schema" ;
66import { client } from "@dariah-eric/images/client" ;
77
@@ -107,18 +107,20 @@ interface CreateOrganisationalUnitParams extends Omit<
107107 slug : string ;
108108 resourceIds ?: Array < string > ;
109109}
110+
110111export async function createOrganisationalUnit ( params : CreateOrganisationalUnitParams ) {
111112 const { imageId, metadata, name, slug, summary, typeId } = params ;
112113
113- const entityType =
114- ( await db . query . entityTypes . findFirst ( {
115- columns : {
116- id : true ,
117- } ,
118- where : { type : "organisational_units" } ,
119- } ) ) ?? undefined ;
114+ const entityType = await db . query . entityTypes . findFirst ( {
115+ columns : {
116+ id : true ,
117+ } ,
118+ where : { type : "organisational_units" } ,
119+ } ) ;
120120
121- if ( ! entityType ) return ;
121+ if ( entityType == null ) {
122+ return null ;
123+ }
122124
123125 const entityStatus = await db . query . entityStatus . findFirst ( {
124126 columns : {
@@ -127,10 +129,12 @@ export async function createOrganisationalUnit(params: CreateOrganisationalUnitP
127129 where : { type : "draft" } ,
128130 } ) ;
129131
130- if ( ! entityStatus ) return ;
132+ if ( entityStatus == null ) {
133+ return null ;
134+ }
131135
132- const entityId = await db . transaction ( async ( tx : Transaction ) => {
133- const entityIds = await tx
136+ const entityId = await db . transaction ( async ( tx ) => {
137+ const [ item ] = await tx
134138 . insert ( schema . entities )
135139 . values ( {
136140 typeId : entityType . id ,
@@ -142,9 +146,11 @@ export async function createOrganisationalUnit(params: CreateOrganisationalUnitP
142146 id : schema . entities . id ,
143147 } ) ;
144148
145- if ( ! entityIds [ 0 ] ) return tx . rollback ( ) ;
149+ if ( item == null ) {
150+ return tx . rollback ( ) ;
151+ }
146152
147- const { id } = entityIds [ 0 ] ;
153+ const { id } = item ;
148154
149155 const organisationalUnit = {
150156 id,
@@ -154,6 +160,7 @@ export async function createOrganisationalUnit(params: CreateOrganisationalUnitP
154160 imageId,
155161 typeId,
156162 } ;
163+
157164 await tx . insert ( schema . organisationalUnits ) . values ( organisationalUnit ) ;
158165
159166 const fieldNamesIds = await tx . query . entityTypesFieldsNames . findMany ( {
@@ -172,7 +179,7 @@ export async function createOrganisationalUnit(params: CreateOrganisationalUnitP
172179
173180 return id ;
174181 } ) ;
175- // decide, what we need to return here
182+
176183 return {
177184 entityId,
178185 } ;
0 commit comments