-
Notifications
You must be signed in to change notification settings - Fork 7
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
1 parent
f9c952f
commit 9f2f89e
Showing
22 changed files
with
562 additions
and
159 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
46 changes: 46 additions & 0 deletions
46
src/database/prisma/migrations/20240529214321_add_inventory_and_expense/migration.sql
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,46 @@ | ||
-- CreateEnum | ||
CREATE TYPE "ExpenseType" AS ENUM ('INCOME', 'EXPENSE'); | ||
|
||
-- CreateEnum | ||
CREATE TYPE "InventoryType" AS ENUM ('MEDICINE', 'FOOD', 'TOYS', 'ACCESSORIES', 'HEALTH_WELLNESS', 'HOUSING', 'LITTER_WASTE_MANAGEMENT', 'APPAREL', 'FEEDING_SUPPLIES', 'TRAVEL_OUTDOOR', 'TRAINING_BEHAVIOR'); | ||
|
||
-- CreateTable | ||
CREATE TABLE "Inventory" ( | ||
"id" TEXT NOT NULL, | ||
"name" TEXT NOT NULL, | ||
"type" "InventoryType" NOT NULL, | ||
"description" TEXT, | ||
"quantity" INTEGER NOT NULL, | ||
"price" DOUBLE PRECISION NOT NULL, | ||
"images" TEXT[], | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" TIMESTAMP(3) NOT NULL, | ||
"userId" TEXT NOT NULL, | ||
|
||
CONSTRAINT "Inventory_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "Expense" ( | ||
"id" TEXT NOT NULL, | ||
"amount" DOUBLE PRECISION NOT NULL, | ||
"date" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"type" "ExpenseType" NOT NULL, | ||
"category" TEXT NOT NULL, | ||
"description" TEXT, | ||
"userId" TEXT NOT NULL, | ||
"inventoryId" TEXT NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" TIMESTAMP(3) NOT NULL, | ||
|
||
CONSTRAINT "Expense_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Inventory" ADD CONSTRAINT "Inventory_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Expense" ADD CONSTRAINT "Expense_inventoryId_fkey" FOREIGN KEY ("inventoryId") REFERENCES "Inventory"("id") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Expense" ADD CONSTRAINT "Expense_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,74 @@ | ||
import path from 'path' | ||
import { promises as fs } from 'fs' | ||
import { handleError } from './handleError' | ||
import path from 'path'; | ||
import { promises as fs } from 'fs'; | ||
import { handleError } from './handleError'; | ||
|
||
declare global { | ||
namespace Express { | ||
interface Response { | ||
locals: { | ||
file?: { | ||
url?: string | ||
urls?: string[] | ||
} | ||
} | ||
url?: string; | ||
urls?: string[]; | ||
}; | ||
}; | ||
} | ||
} | ||
} | ||
|
||
const bucketRoute = (originalUrl: string) => { | ||
console.log(originalUrl); | ||
if (originalUrl.includes('/api/v1/pets')) { | ||
return 'pets'; | ||
} else if (originalUrl.includes('/api/v1/user')) { | ||
return 'users/avatar'; // Assuming you want to maintain this more specific path for users | ||
} else if (originalUrl.includes('/api/v1/vaccines/petVaccine/')) { | ||
return 'vaccines'; // Assuming you want to maintain this more specific path for users | ||
} else if (originalUrl.includes('/api/v1/inventory')) { | ||
return 'inventory'; // Assuming you want to maintain this more specific path for users | ||
} else if (originalUrl.includes('qrCode')) { | ||
return 'qrCode'; | ||
} | ||
return ''; | ||
} | ||
}; | ||
|
||
export const saveToLocal = async ({ | ||
req, | ||
res, | ||
fieldName, | ||
}: { | ||
req: any | ||
res: any | ||
fieldName: string | ||
req: any; | ||
res: any; | ||
fieldName: string; | ||
}) => { | ||
const uploadsDir = process.env.DEV === 'true' ? path.join(__dirname, '../..', `uploads/${bucketRoute(req.originalUrl)}`) : `${process.env.UPLOAD_DIR}/${bucketRoute(req.originalUrl)}` || 'uploads' | ||
const uploadsDir = | ||
process.env.DEV === 'true' | ||
? path.join(__dirname, '../..', `uploads/${bucketRoute(req.originalUrl)}`) | ||
: `${process.env.UPLOAD_DIR}/${bucketRoute(req.originalUrl)}` || 'uploads'; | ||
|
||
try { | ||
await fs.access(uploadsDir) | ||
await fs.access(uploadsDir); | ||
} catch { | ||
await fs.mkdir(uploadsDir, { recursive: true }) | ||
await fs.mkdir(uploadsDir, { recursive: true }); | ||
} | ||
|
||
const urls: string[] = [] | ||
const urls: string[] = []; | ||
for (const file of req.files) { | ||
const uniqueName = `${Date.now()}-pets-love` | ||
const localFilePath = path.join(uploadsDir, uniqueName) | ||
const uniqueName = `${Date.now()}-pets-love`; | ||
const localFilePath = path.join(uploadsDir, uniqueName); | ||
|
||
try { | ||
await fs.writeFile(localFilePath, file.buffer) | ||
urls.push(uniqueName) | ||
await fs.writeFile(localFilePath, file.buffer); | ||
urls.push(uniqueName); | ||
} catch (error: any) { | ||
handleError({ | ||
res, | ||
error, | ||
status: 500, | ||
message: 'Failed to save the image locally.', | ||
}) | ||
}); | ||
} | ||
} | ||
|
||
res.locals.file = | ||
urls.length === 1 | ||
? { [fieldName]: { url: urls[0] } } | ||
: { [fieldName]: { urls } } | ||
} | ||
urls.length === 1 ? { [fieldName]: { url: urls[0] } } : { [fieldName]: { urls } }; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { | ||
createInventory, | ||
updateInventory, | ||
getInventories, | ||
getInventory, | ||
deleteInventory, | ||
} from '../useCases/inventoryCase/inventory.controller' | ||
import express from 'express' | ||
|
||
import { verifyToken } from '../middlewares/auth' | ||
import { createCloudUploader } from '../middlewares/cloudUploader' | ||
|
||
const uploadImagesInventory = createCloudUploader('newImages') | ||
|
||
const router = express.Router() | ||
|
||
// CREATE INVENTORY ITEM | ||
router.post('/inventory', [verifyToken, uploadImagesInventory], createInventory) | ||
|
||
// UPDATE INVENTORY ITEM | ||
router.put( | ||
'/inventory/:inventoryId', | ||
[verifyToken, uploadImagesInventory], | ||
updateInventory, | ||
) | ||
|
||
// GET ALL INVENTORY ITEMS | ||
router.get('/inventory', [verifyToken], getInventories) | ||
|
||
// GET INVENTORY ITEM BY ID | ||
router.get('/inventory/:inventoryId', [verifyToken], getInventory) | ||
|
||
// DELETE INVENTORY ITEM BY ID | ||
router.delete('/inventory/:inventoryId', [verifyToken], deleteInventory) | ||
|
||
export default router |
Oops, something went wrong.