-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
268 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
apps/api/src/http/routers/admin/food-thumbnail-images.router.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { initServer } from '@ts-rest/express'; | ||
import multer from 'multer'; | ||
import { ValidationError } from '@intake24/api/http/errors'; | ||
import { permission } from '@intake24/api/http/middleware'; | ||
import ioc from '@intake24/api/ioc'; | ||
import { contract } from '@intake24/common/contracts'; | ||
import { imageMulterFile } from '@intake24/common/types/http/admin/source-images'; | ||
import { FoodLocal } from '@intake24/db'; | ||
import { customTypeValidationMessage } from '../../requests/util'; | ||
|
||
export function foodThumbnailImages() { | ||
const upload = multer({ dest: ioc.cradle.fsConfig.local.uploads }); | ||
|
||
// FIXME: Empty form produces HTTP 500 with 'Unexpected end of form' message | ||
// instead of 400 | ||
return initServer().router(contract.admin.foodThumbnailImages, { | ||
update: { | ||
middleware: [ | ||
permission('fdbs:edit'), | ||
upload.single('image'), | ||
], | ||
handler: async ({ file, params: { localeId, foodCode }, req }) => { | ||
const { | ||
user, | ||
foodThumbnailImageService, | ||
} = req.scope.cradle; | ||
|
||
const res = imageMulterFile.safeParse(file); | ||
|
||
if (!res.success) { | ||
throw new ValidationError( | ||
customTypeValidationMessage('file._', { req, path: 'image' }), | ||
{ path: 'image' }, | ||
); | ||
} | ||
|
||
const foodLocal = await FoodLocal.findOne({ where: { localeId, foodCode } }); | ||
|
||
if (foodLocal === null) | ||
return { status: 404, body: undefined }; | ||
|
||
await foodThumbnailImageService.createImage(user.userId, foodLocal.id, res.data); | ||
|
||
return { status: 200, body: undefined }; | ||
}, | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
apps/api/src/services/admin/images/food-thumbnail-image.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { IoC } from '@intake24/api/ioc'; | ||
import type { SourceFileInput } from '@intake24/common/types/http/admin'; | ||
import { FoodThumbnailImage } from '@intake24/db'; | ||
|
||
function foodThumbnailImageService({ | ||
processedImageService, | ||
sourceImageService, | ||
}: Pick<IoC, 'processedImageService' | 'sourceImageService'>) { | ||
const createImage = async (uploaderId: string, foodLocalId: string, sourceImageInput: SourceFileInput): Promise<FoodThumbnailImage> => { | ||
const sourceImage = await sourceImageService.uploadSourceImage({ file: sourceImageInput, uploader: uploaderId, id: foodLocalId }, 'food_thumbnail'); | ||
const thumbnailImage = await processedImageService.createFoodThumbnailImage(foodLocalId, sourceImage); | ||
|
||
// According to https://sequelize.org/docs/v6/other-topics/upgrade/ | ||
// created is always null in Postgres | ||
const [instance, _] = await FoodThumbnailImage.upsert({ | ||
foodLocalId, | ||
imageId: thumbnailImage.id, | ||
}); | ||
|
||
return instance; | ||
}; | ||
|
||
return { | ||
createImage, | ||
}; | ||
} | ||
|
||
export default foodThumbnailImageService; | ||
|
||
export type FoodThumbnailImageService = ReturnType<typeof foodThumbnailImageService>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
packages/common/src/contracts/admin/fdbs/food-thumbnail-images.contract.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { initContract } from '@ts-rest/core'; | ||
import { z } from 'zod'; | ||
|
||
export const foodThumbnailImages = initContract().router({ | ||
update: { | ||
method: 'PUT', | ||
path: '/admin/fdbs/:localeId/:foodCode/thumbnail', | ||
contentType: 'multipart/form-data', | ||
body: z.object({ | ||
image: z.custom<File>(), | ||
}), | ||
pathParams: z.object({ localeId: z.string(), foodCode: z.string() }), | ||
responses: { | ||
200: z.undefined(), | ||
404: z.undefined(), | ||
}, | ||
summary: 'Update food image thumbnail', | ||
description: 'Update food image thumbnail', | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
packages/db/sequelize/foods/migrations/20250204153931-create_food_thumbnail_images.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
up: async (queryInterface, Sequelize) => { | ||
await queryInterface.createTable('food_thumbnail_images', { | ||
id: { | ||
type: Sequelize.BIGINT, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
}, | ||
food_local_id: { | ||
type: Sequelize.BIGINT, | ||
allowNull: false, | ||
unique: true, | ||
references: { | ||
model: 'food_locals', | ||
key: 'id', | ||
}, | ||
onUpdate: 'CASCADE', | ||
onDelete: 'CASCADE', | ||
}, | ||
image_id: { | ||
type: Sequelize.BIGINT, | ||
allowNull: false, | ||
references: { | ||
model: 'processed_images', | ||
key: 'id', | ||
}, | ||
onUpdate: 'CASCADE', | ||
onDelete: 'CASCADE', | ||
}, | ||
created_at: { | ||
type: Sequelize.DATE, | ||
allowNull: false, | ||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), | ||
}, | ||
updated_at: { | ||
type: Sequelize.DATE, | ||
allowNull: false, | ||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), | ||
}, | ||
}); | ||
|
||
await queryInterface.addIndex('food_thumbnail_images', ['food_local_id']); | ||
}, | ||
|
||
down: async (queryInterface) => { | ||
await queryInterface.dropTable('food_thumbnail_images'); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.