Skip to content

Commit f30b9f7

Browse files
committed
fix(order): gcash mop not working
1 parent 3b65ec4 commit f30b9f7

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

src/api/photos.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,16 @@ async function getPhotos(context: ElysiaContext) {
3737
}
3838

3939
try {
40-
// Get photo
41-
const photo = await Photo.getByHash(hash);
40+
// Photo
41+
let photo = null;
42+
43+
// If from order reference
44+
if (context.path.includes("gcash")) {
45+
photo = await Photo.getByReference(hash);
46+
} else {
47+
photo = await Photo.getByHash(hash);
48+
}
49+
4250
// Set content type
4351
setHeader(context, 'content-type', photo.type);
4452
// Return photo

src/db/models/photo.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,45 @@ class Photo {
5353
});
5454
}
5555

56+
/**
57+
* Get photo by reference
58+
* @param reference Photo reference
59+
*/
60+
public static getByReference(reference: string): Promise<File> {
61+
return new Promise(async (resolve, reject) => {
62+
// Get database pool connection
63+
const db = Database.getInstance();
64+
65+
// Get photo
66+
try {
67+
const result = await db.query<PhotoModel[]>(
68+
"SELECT * FROM photos_gcash WHERE reference = ? LIMIT 1", [reference]
69+
);
70+
71+
// If no photo found
72+
if (result.length === 0) {
73+
Log.e("No photo found");
74+
reject(ErrorTypes.DB_EMPTY_RESULT);
75+
return;
76+
}
77+
78+
// Create File object
79+
const photo = new File([result[0].data], result[0].name || "unknown", {
80+
type: result[0].type
81+
});
82+
83+
// Return photo
84+
resolve(photo);
85+
}
86+
87+
// Log error and reject promise
88+
catch (e) {
89+
Log.e(e);
90+
reject(ErrorTypes.DB_ERROR);
91+
}
92+
});
93+
}
94+
5695
/**
5796
* Insert photo to the database
5897
* @param
@@ -81,9 +120,9 @@ class Photo {
81120
// If reference is defined
82121
if (reference) {
83122
// Query (reference)
84-
query = "INSERT INTO gcash_uploads (hash, reference, name, type, data, date_stamp) VALUES (?, ?, ?, ?, ?, NOW())";
123+
query = "INSERT INTO photos_gcash (hash, reference, name, type, data, date_stamp) VALUES (?, ?, ?, ?, ?, NOW())";
85124
// Add reference to data at the beginning
86-
data.unshift(reference);
125+
data = [hash, reference, photo.name || null, photo.type, Buffer.from(await photo.arrayBuffer())];
87126
}
88127

89128
// Insert photo

src/db/structure.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export enum Tables {
1313
STUDENTS = "students",
1414
TUTORIALS = "tutorials",
1515
VARIATIONS = "variations",
16-
GCASH_UPLOADS = "gcash_uploads",
16+
PHOTOS_GCASH = "photos_gcash",
1717
}
1818

1919
export enum AnnouncementsColumn {

src/routes/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ const routes: AppRoutes[] = [
7878
{ path: "/orders/guest", methods: ["GET"], handler: orders },
7979
{ path: "/orders", methods: ["GET", "POST"], handler: orders, auth: { GET: [ AuthType.ADMIN, AuthType.STUDENT ] }},
8080

81+
{ path: "/photos/:hash/gcash", methods: ["GET"], handler: photos },
8182
{ path: "/photos/:hash", methods: ["GET"], handler: photos },
8283
{ path: "/photos", methods: ["POST"], handler: photos },
8384

0 commit comments

Comments
 (0)