Skip to content

Commit 5829be8

Browse files
committed
adding keycount to keyloans and setting key creation limit in keyloan.tsx
1 parent 471702b commit 5829be8

File tree

8 files changed

+61
-36
lines changed

8 files changed

+61
-36
lines changed

apps/keys-portal/src/components/key-loans/KeyLoansTable.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -371,12 +371,7 @@ export function KeyLoansTable({
371371
</TableCell>
372372
<TableCell>
373373
<Badge variant="secondary">
374-
{isExpanded && !isLoadingThis && expansion.loadedData
375-
? (expansion.loadedData.loanDetails.keysArray
376-
?.length || 0) +
377-
(expansion.loadedData.loanDetails.keyCardsArray
378-
?.length || 0)
379-
: '-'}
374+
{(loan.keyCount ?? 0) + (loan.cardCount ?? 0)}
380375
</Badge>
381376
</TableCell>
382377
<TableCell>

apps/keys-portal/src/components/loan/AddKeyForm.tsx

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -144,21 +144,14 @@ export function AddKeyForm({
144144
keys.find((k) => k.rentalObjectCode === rentalObjectCode)?.flexNumber ??
145145
1
146146

147-
const defaultKeyType: KeyType = 'LGH'
148-
const defaultKeyName = `${defaultKeyType}-1`
149-
150147
setRows([
151148
{
152149
id: crypto.randomUUID(),
153-
keyType: defaultKeyType,
154-
keyName: defaultKeyName,
150+
keyType: 'LGH',
151+
keyName: '',
155152
flexNumber: defaultFlexNumber,
156153
quantity: 1,
157-
startingSequenceNumber: calculateNextSequenceNumber(
158-
defaultKeyType,
159-
defaultKeyName,
160-
defaultFlexNumber
161-
),
154+
startingSequenceNumber: 1,
162155
keySystemId: selectedKeySystem?.id,
163156
},
164157
])
@@ -222,23 +215,15 @@ export function AddKeyForm({
222215
const defaultFlexNumber =
223216
keys.find((k) => k.rentalObjectCode === rentalObjectCode)?.flexNumber ?? 1
224217

225-
const defaultKeyType: KeyType = 'LGH'
226-
const defaultKeyName = `${defaultKeyType}-1`
227-
228218
setRows([
229219
...rows,
230220
{
231221
id: crypto.randomUUID(),
232-
keyType: defaultKeyType,
233-
keyName: defaultKeyName,
222+
keyType: 'LGH',
223+
keyName: '',
234224
flexNumber: defaultFlexNumber,
235225
quantity: 1,
236-
startingSequenceNumber: calculateNextSequenceNumber(
237-
defaultKeyType,
238-
defaultKeyName,
239-
defaultFlexNumber,
240-
rows // Pass current rows to check for conflicts
241-
),
226+
startingSequenceNumber: 1,
242227
keySystemId: selectedKeySystem?.id,
243228
},
244229
])
@@ -252,7 +237,7 @@ export function AddKeyForm({
252237
setRows(
253238
rows.map((r) => {
254239
if (r.id === id) {
255-
const newQuantity = Math.max(1, r.quantity + delta)
240+
const newQuantity = Math.min(20, Math.max(1, r.quantity + delta))
256241
return { ...r, quantity: newQuantity }
257242
}
258243
return r
@@ -299,8 +284,8 @@ export function AddKeyForm({
299284
const invalidRows = rows.filter((r) => !r.keyName.trim())
300285
if (invalidRows.length > 0) {
301286
toast({
302-
title: 'Validation Error',
303-
description: 'All rows must have a key name',
287+
title: 'Valideringsfel',
288+
description: 'Alla rader måste ha ett nyckelnamn',
304289
variant: 'destructive',
305290
})
306291
return
@@ -502,7 +487,7 @@ export function AddKeyForm({
502487
size="sm"
503488
variant="outline"
504489
onClick={() => handleQuantityChange(row.id, 1)}
505-
disabled={isSubmitting}
490+
disabled={isSubmitting || row.quantity >= 20}
506491
className="h-8 w-8 p-0"
507492
>
508493
<Plus className="h-3 w-3" />

apps/keys-portal/src/services/api/core/generated/api-types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9366,6 +9366,8 @@ export interface components {
93669366
updatedAt: string;
93679367
createdBy?: string | null;
93689368
updatedBy?: string | null;
9369+
keyCount?: number;
9370+
cardCount?: number;
93699371
};
93709372
KeyDetails: {
93719373
/** Format: uuid */
@@ -9423,6 +9425,8 @@ export interface components {
94239425
updatedAt: string;
94249426
createdBy?: string | null;
94259427
updatedBy?: string | null;
9428+
keyCount?: number;
9429+
cardCount?: number;
94269430
keysArray: ({
94279431
/** Format: uuid */
94289432
id: string;
@@ -9477,6 +9481,8 @@ export interface components {
94779481
updatedAt: components["schemas"]["KeyLoanWithDetails"]["updatedAt"];
94789482
createdBy?: components["schemas"]["KeyLoanWithDetails"]["createdBy"];
94799483
updatedBy?: components["schemas"]["KeyLoanWithDetails"]["updatedBy"];
9484+
keyCount?: components["schemas"]["KeyLoanWithDetails"]["keyCount"];
9485+
cardCount?: components["schemas"]["KeyLoanWithDetails"]["cardCount"];
94809486
}[] | null;
94819487
events?: (({
94829488
/** Format: uuid */
@@ -9977,6 +9983,8 @@ export interface components {
99779983
updatedAt: string;
99789984
createdBy?: string | null;
99799985
updatedBy?: string | null;
9986+
keyCount?: number;
9987+
cardCount?: number;
99809988
})[]) | null;
99819989
};
99829990
QueryCardOwnersParams: {

apps/keys-portal/src/services/api/generated/api-types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2552,6 +2552,8 @@ export interface components {
25522552
updatedAt: string;
25532553
createdBy?: string | null;
25542554
updatedBy?: string | null;
2555+
keyCount?: number;
2556+
cardCount?: number;
25552557
})[]) | null;
25562558
};
25572559
CreateKeyRequest: {
@@ -2659,6 +2661,8 @@ export interface components {
26592661
updatedAt: string;
26602662
createdBy?: string | null;
26612663
updatedBy?: string | null;
2664+
keyCount?: number;
2665+
cardCount?: number;
26622666
};
26632667
KeyDetails: {
26642668
/** Format: uuid */
@@ -2721,6 +2725,8 @@ export interface components {
27212725
updatedAt: string;
27222726
createdBy?: string | null;
27232727
updatedBy?: string | null;
2728+
keyCount?: number;
2729+
cardCount?: number;
27242730
})[]) | null;
27252731
events?: (({
27262732
/** Format: uuid */
@@ -2850,6 +2856,8 @@ export interface components {
28502856
updatedAt: components["schemas"]["KeyLoan"]["updatedAt"];
28512857
createdBy?: components["schemas"]["KeyLoan"]["createdBy"];
28522858
updatedBy?: components["schemas"]["KeyLoan"]["updatedBy"];
2859+
keyCount?: components["schemas"]["KeyLoan"]["keyCount"];
2860+
cardCount?: components["schemas"]["KeyLoan"]["cardCount"];
28532861
keysArray: components["schemas"]["KeyDetails"][];
28542862
keyCardsArray: components["schemas"]["Card"][];
28552863
receipts: components["schemas"]["Receipt"][];

apps/property-tree/src/services/api/core/generated/api-types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9366,6 +9366,8 @@ export interface components {
93669366
updatedAt: string;
93679367
createdBy?: string | null;
93689368
updatedBy?: string | null;
9369+
keyCount?: number;
9370+
cardCount?: number;
93699371
};
93709372
KeyDetails: {
93719373
/** Format: uuid */
@@ -9423,6 +9425,8 @@ export interface components {
94239425
updatedAt: string;
94249426
createdBy?: string | null;
94259427
updatedBy?: string | null;
9428+
keyCount?: number;
9429+
cardCount?: number;
94269430
keysArray: ({
94279431
/** Format: uuid */
94289432
id: string;
@@ -9477,6 +9481,8 @@ export interface components {
94779481
updatedAt: components["schemas"]["KeyLoanWithDetails"]["updatedAt"];
94789482
createdBy?: components["schemas"]["KeyLoanWithDetails"]["createdBy"];
94799483
updatedBy?: components["schemas"]["KeyLoanWithDetails"]["updatedBy"];
9484+
keyCount?: components["schemas"]["KeyLoanWithDetails"]["keyCount"];
9485+
cardCount?: components["schemas"]["KeyLoanWithDetails"]["cardCount"];
94809486
}[] | null;
94819487
events?: (({
94829488
/** Format: uuid */
@@ -9977,6 +9983,8 @@ export interface components {
99779983
updatedAt: string;
99789984
createdBy?: string | null;
99799985
updatedBy?: string | null;
9986+
keyCount?: number;
9987+
cardCount?: number;
99809988
})[]) | null;
99819989
};
99829990
QueryCardOwnersParams: {

core/src/adapters/keys-adapter/generated/api-types.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2552,6 +2552,8 @@ export interface components {
25522552
updatedAt: string
25532553
createdBy?: string | null
25542554
updatedBy?: string | null
2555+
keyCount?: number
2556+
cardCount?: number
25552557
}[]
25562558
| null
25572559
}
@@ -2693,6 +2695,8 @@ export interface components {
26932695
updatedAt: string
26942696
createdBy?: string | null
26952697
updatedBy?: string | null
2698+
keyCount?: number
2699+
cardCount?: number
26962700
}
26972701
KeyDetails: {
26982702
/** Format: uuid */
@@ -2767,14 +2771,16 @@ export interface components {
27672771
updatedAt: string
27682772
createdBy?: string | null
27692773
updatedBy?: string | null
2774+
keyCount?: number
2775+
cardCount?: number
27702776
}[]
27712777
| null
27722778
events?:
27732779
| {
27742780
/** Format: uuid */
27752781
id: string
27762782
/** @enum {string} */
2777-
type: 'FLEX' | 'ORDER' | 'LOST'
2783+
type: 'FLEX' | 'ORDER' | 'LOST' | 'REPLACEMENT'
27782784
/** @enum {string} */
27792785
status: 'ORDERED' | 'RECEIVED' | 'COMPLETED'
27802786
/** Format: uuid */
@@ -2899,6 +2905,8 @@ export interface components {
28992905
updatedAt: components['schemas']['KeyLoan']['updatedAt']
29002906
createdBy?: components['schemas']['KeyLoan']['createdBy']
29012907
updatedBy?: components['schemas']['KeyLoan']['updatedBy']
2908+
keyCount?: components['schemas']['KeyLoan']['keyCount']
2909+
cardCount?: components['schemas']['KeyLoan']['cardCount']
29022910
keysArray: components['schemas']['KeyDetails'][]
29032911
keyCardsArray: components['schemas']['Card'][]
29042912
receipts: components['schemas']['Receipt'][]
@@ -2992,7 +3000,7 @@ export interface components {
29923000
CreateKeyEventRequest: {
29933001
keys: string[]
29943002
/** @enum {string} */
2995-
type: 'FLEX' | 'ORDER' | 'LOST'
3003+
type: 'FLEX' | 'ORDER' | 'LOST' | 'REPLACEMENT'
29963004
/** @enum {string} */
29973005
status: 'ORDERED' | 'RECEIVED' | 'COMPLETED'
29983006
/** Format: uuid */
@@ -3001,7 +3009,7 @@ export interface components {
30013009
UpdateKeyEventRequest: {
30023010
keys?: string[]
30033011
/** @enum {string} */
3004-
type?: 'FLEX' | 'ORDER' | 'LOST'
3012+
type?: 'FLEX' | 'ORDER' | 'LOST' | 'REPLACEMENT'
30053013
/** @enum {string} */
30063014
status?: 'ORDERED' | 'RECEIVED' | 'COMPLETED'
30073015
/** Format: uuid */
@@ -3011,7 +3019,7 @@ export interface components {
30113019
/** Format: uuid */
30123020
id: string
30133021
/** @enum {string} */
3014-
type: 'FLEX' | 'ORDER' | 'LOST'
3022+
type: 'FLEX' | 'ORDER' | 'LOST' | 'REPLACEMENT'
30153023
/** @enum {string} */
30163024
status: 'ORDERED' | 'RECEIVED' | 'COMPLETED'
30173025
/** Format: uuid */

libs/types/src/keys/schema.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ export const KeyLoanSchema = z.object({
7070
updatedAt: z.coerce.date(),
7171
createdBy: z.string().nullable().optional(),
7272
updatedBy: z.string().nullable().optional(),
73+
keyCount: z.number().optional(),
74+
cardCount: z.number().optional(),
7375
})
7476

7577
export const KeySystemSchema = z.object({

services/keys/src/services/key-service/adapters/key-loans-adapter.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,18 @@ export function getKeyLoansSearchQuery(
276276
options: KeyLoansSearchOptions = {},
277277
dbConnection: Knex | Knex.Transaction = db
278278
): Knex.QueryBuilder {
279-
let query = dbConnection(TABLE).select(`${TABLE}.*`)
279+
let query = dbConnection(TABLE)
280+
.select(`${TABLE}.*`)
281+
.select(
282+
dbConnection.raw(
283+
`(SELECT COUNT(*) FROM key_loan_keys WHERE keyLoanId = ${TABLE}.id) as keyCount`
284+
)
285+
)
286+
.select(
287+
dbConnection.raw(
288+
`(SELECT COUNT(*) FROM key_loan_cards WHERE keyLoanId = ${TABLE}.id) as cardCount`
289+
)
290+
)
280291

281292
// Filter by key name, rental object code, or contact
282293
if (options.keyNameOrObjectCode) {

0 commit comments

Comments
 (0)