Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/v1/users/controllers/updateUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import { Hono } from 'hono';
import { z } from 'zod';

import { prisma } from '../../../lib/prisma';
import {
forbiddenError,
internalServerError,
notFoundError
} from '../../../lib/errorMessages';
import { forbiddenError, internalServerError, notFoundError } from '../../../lib/errorMessages';
import { validatePermissions } from '../../../lib/util';
import { bodyValidator, idParamValidator } from '../../../lib/validators';

Expand Down
1 change: 1 addition & 0 deletions src/v1/users/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default new Hono()
.route('/logout', logoutUser)
.route('/current', getCurrentUser)
.route('/:id', updateUser)
.route('/:id', deleteUser)
.route('/', getAllUsers)
.route('/', createUser)
.route('/', deleteUser);
9 changes: 0 additions & 9 deletions src/v2/assets/controllers/servers/createServerAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,6 @@ export default new Hono().post(
select: {
model: true
}
},
storage: {
include: {
asset: {
select: {
name: true
}
}
}
}
}
});
Expand Down
9 changes: 0 additions & 9 deletions src/v2/assets/controllers/servers/getAllServerAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@ export default new Hono().get('/', paginationQueryValidator({}), async (c) => {
select: {
model: true
}
},
storage: {
include: {
asset: {
select: {
name: true
}
}
}
}
}
}),
Expand Down
9 changes: 0 additions & 9 deletions src/v2/assets/controllers/servers/getServerAssetById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ export default new Hono().get('/', idParamValidator({}), async (c) => {
}
},
include: {
storage: {
include: {
asset: {
select: {
name: true
}
}
}
},
...assetInclude
}
});
Expand Down
11 changes: 1 addition & 10 deletions src/v2/assets/controllers/servers/updateServerAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,7 @@ export default new Hono().patch(
}
},
include: {
...assetInclude,
storage: {
include: {
asset: {
select: {
name: true
}
}
}
}
...assetInclude
}
});

Expand Down
9 changes: 0 additions & 9 deletions src/v2/assets/controllers/storages/createStorageAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,6 @@ export default new Hono().post(
select: {
size: true
}
},
storage: {
include: {
asset: {
select: {
name: true
}
}
}
}
}
});
Expand Down
9 changes: 0 additions & 9 deletions src/v2/assets/controllers/storages/getAllStorageAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,6 @@ export default new Hono().get('/', paginationQueryValidator({}), async (c) => {
select: {
size: true
}
},
storage: {
include: {
asset: {
select: {
name: true
}
}
}
}
}
}),
Expand Down
24 changes: 14 additions & 10 deletions src/v2/assets/controllers/storages/getStorageAssetById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@ export default new Hono().get('/', idParamValidator({}), async (c) => {
}
},
include: {
storage: {
include: {
asset: {
select: {
name: true
}
}
...assetInclude,
storageType: {
select: {
size: true
}
},
...assetInclude
}
}
});

Expand All @@ -37,7 +33,15 @@ export default new Hono().get('/', idParamValidator({}), async (c) => {
return notFoundError(c);
}

return c.json(serializeAsset({ ...storage, jsonPosition: 0 }), 200);
return c.json(
serializeAsset(
{ ...storage, jsonPosition: 0 },
{
size: storage.storageType?.size
}
),
200
);
} catch (err) {
return internalServerError(c, err);
}
Expand Down
40 changes: 27 additions & 13 deletions src/v2/assets/controllers/storages/updateStorageAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { prisma } from '../../../../lib/prisma';
import { internalServerError, notFoundError } from '../../../../lib/errorMessages';
import { assetInclude, serializeAsset } from '../../lib/util';
import { bodyValidator, idParamValidator } from '../../../../lib/validators';
import { z } from 'zod';
import { size, z } from 'zod';

export default new Hono().patch(
'/',
Expand All @@ -28,6 +28,11 @@ export default new Hono().patch(
.number({ error: 'Group ID must be a number' })
.int({ error: 'Group ID must be an integer' })
.positive({ error: 'Group ID must be greater than 0' })
.optional(),
size: z
.number({ error: 'Size must be a number' })
.int({ error: 'Size must be an integer' })
.positive({ error: 'Size must be greater than 0' })
.optional()
})
.refine((data) => Object.keys(data).length > 0, {
Expand All @@ -49,7 +54,12 @@ export default new Hono().patch(
}
},
include: {
...assetInclude
...assetInclude,
storageType: {
select: {
size: true
}
}
}
});

Expand Down Expand Up @@ -82,23 +92,27 @@ export default new Hono().patch(
notes: body.notes ?? existingStorage.notes,
storageId: body.storageId ?? existingStorage.storageId,
position: body.position ?? existingStorage.position,
groupId: body.groupId ?? existingStorage.groupId
},
include: {
...assetInclude,
storage: {
include: {
asset: {
select: {
name: true
}
groupId: body.groupId ?? existingStorage.groupId,
storageType: {
update: {
data: {
size: body.size ?? existingStorage.storageType?.size
}
}
}
},
include: {
...assetInclude
}
});

return c.json(serializeAsset({ ...updatedStorage, jsonPosition: 0 }), 200);
return c.json(
serializeAsset(
{ ...updatedStorage, jsonPosition: 0 },
{ size: existingStorage.storageType?.size }
),
200
);
} catch (err) {
return internalServerError(c, err);
}
Expand Down
86 changes: 86 additions & 0 deletions src/v2/assets/controllers/ups/createUpsAsset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { Hono } from 'hono';
import { z } from 'zod';

import { prisma } from '../../../../lib/prisma';
import {
existingResourceError,
forbiddenError,
internalServerError
} from '../../../../lib/errorMessages';
import { validatePermissions } from '../../../../lib/util';
import { assetInclude, buildBaseAssetSchema, serializeAsset } from '../../lib/util';
import { assetSchema } from '../../lib/validator';
import { bodyValidator } from '../../../../lib/validators';

export default new Hono().post(
'/',
bodyValidator(
assetSchema.extend({
capacity: z
.number({ error: 'Capacity must be a number' })
.positive({ error: 'Capacity must be greater than 0' })
.default(1)
})
),
async (c) => {
try {
// Check user permissions
if (!validatePermissions(['asset.create'], c)) {
return forbiddenError(c);
}

// Get request information
const body = c.req.valid('json');

// Try and get the ups asset from the database
const existingUps = await prisma.asset.findFirst({
where: {
name: body.name,
ups: {
isNot: null
}
},
select: {
id: true
}
});

// Check if a ups exists
if (existingUps) {
return existingResourceError(c);
}

// Add the new ups to the database
const newUps = await prisma.asset.create({
data: {
...buildBaseAssetSchema(body),
ups: {
create: {
capacity: body.capacity
}
}
},
include: {
...assetInclude,
ups: {
select: {
capacity: true
}
}
}
});

return c.json(
serializeAsset(
{ ...newUps, jsonPosition: 0 },
{
capacity: newUps.ups?.capacity
}
),
201
);
} catch (err) {
return internalServerError(c, err);
}
}
);
54 changes: 54 additions & 0 deletions src/v2/assets/controllers/ups/getAllUpsAssets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Hono } from 'hono';

import { prisma } from '../../../../lib/prisma';
import { internalServerError } from '../../../../lib/errorMessages';
import { assetInclude, serializeAsset } from '../../lib/util';
import { paginationQueryValidator } from '../../../../lib/validators';

export default new Hono().get('/', paginationQueryValidator({}), async (c) => {
try {
// Get information from the request
const { page, limit } = c.req.valid('query');

// Get all the storages
const [upses, total] = await prisma.$transaction([
prisma.asset.findMany({
where: {
ups: {
isNot: null
}
},
include: {
...assetInclude,
ups: {
select: {
capacity: true
}
}
}
}),

prisma.storage.count()
]);

return c.json(
{
upses: upses.map((ups) => ({
...serializeAsset(
{ ...ups, jsonPosition: 0 },
{
capacity: ups.ups?.capacity
}
)
})),
page,
limit,
total,
totalPage: Math.ceil(total / limit)
},
200
);
} catch (err) {
return internalServerError(c, err);
}
});
Loading
Loading