Skip to content

Commit

Permalink
Fix image upload await
Browse files Browse the repository at this point in the history
  • Loading branch information
Rikthepixel committed Jan 19, 2024
1 parent 2a29bca commit 79eaa05
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 11 deletions.
5 changes: 5 additions & 0 deletions apps/accounts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { config as loadEnv } from 'dotenv';

loadEnv();
loadEnv({ path: '.env.local', override: true });
if (process.env.NODE_ENV === 'production') {
loadEnv({ path: '.env.production', override: true });
}

import Koa from 'koa';

Expand Down
5 changes: 5 additions & 0 deletions apps/advertisements/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { config as loadEnv } from 'dotenv';

loadEnv();
loadEnv({ path: '.env.local', override: true });
if (process.env.NODE_ENV === 'production') {
loadEnv({ path: '.env.production', override: true });
}

import Koa from 'koa';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ export default class KnexAdvertisementRepository

if (!ad) return null;

const images = await trx
.table('images as imgs')
.where('imgs.advertisement_id', ad.id)
.select<UidsToBuffers<Pick<Image, 'uid'>>[]>('imgs.uid')
const imagesTask = trx
.table('images')
.where('advertisement_id', ad.id)
.select<UidsToBuffers<Pick<Image, 'uid'>>[]>('uid')
.orderBy('id', 'desc')
.then((imgs) => imgs.map((img) => trx.fn.binToUuid(img.uid)));

const propertyValues = await trx
const propertyValuesTask = trx
.table('category_property_option_values as vals')
.where('vals.advertisement_id', ad.id)
.join(
Expand Down Expand Up @@ -131,6 +131,8 @@ export default class KnexAdvertisementRepository
})),
);

const [images, propertyValues] = await Promise.all([imagesTask, propertyValuesTask])

return {
...ad,
images,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export default class AzureImageRepository implements ImageRepository {
advertisementId: number,
images: UploadableImage[],
): Promise<void> {
this.db.transaction(async (trx) => {
await this.db.transaction(async (trx) => {

await Promise.all([
trx.table('images').insert(
images.map((img) => ({
Expand Down
6 changes: 6 additions & 0 deletions apps/advertisements/src/services/AdvertisementService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export default class AdvertisementService {

const images = files.map((img) => ({ ...img, uid: randomUUID() }));
await this.imageRepository.upload(advertisementId, images);
console.log(await this.imageRepository.getByAdvertisement(advertisementId));
return images.map((img) => img.uid);
}

Expand Down Expand Up @@ -128,6 +129,11 @@ export default class AdvertisementService {
})
: null;

console.log({
adImages: advertisement.images,
changedImages: changes.images,
});

const allRemainingImagesValid = changes.images.every((img) =>
advertisement.images.includes(img),
);
Expand Down
2 changes: 1 addition & 1 deletion apps/frontend/src/apis/ads/manage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export async function putAdvertisement(
});
}

export async function uploadAdvertisementImage(uid: string, images: File[]) {
export async function uploadAdvertisementImages(uid: string, images: File[]) {
const request = new FormData();
for (const image of images) {
request.append('image', image);
Expand Down
6 changes: 4 additions & 2 deletions apps/frontend/src/stores/useAdvertisementEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
createAdvertisement,
getAdvertisement,
putAdvertisement,
uploadAdvertisementImage,
uploadAdvertisementImages,
} from '@/apis/ads/manage';
import Resource from '@/helpers/Resource';
import { FileWithPath } from '@mantine/dropzone';
Expand Down Expand Up @@ -121,12 +121,14 @@ const useAdvertisementEditor = create<AdvertisementEditorState>((set, get) => ({
const { imagesToUpload, images, ...edited } = state.edited;
const uploadedImages =
imagesToUpload.length > 0
? await uploadAdvertisementImage(ad.uid, imagesToUpload).catch((e) => {
? await uploadAdvertisementImages(ad.uid, imagesToUpload).catch((e) => {
set({ isSaving: false });
throw e;
})
: [];

console.log(uploadedImages)

await putAdvertisement(ad.uid, {
...edited,
images: [...uploadedImages, ...images],
Expand Down
8 changes: 6 additions & 2 deletions apps/messages/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { config as loadEnv } from 'dotenv';

loadEnv();
loadEnv({ path: '.env.local', override: true });
if (process.env.NODE_ENV === 'production') {
loadEnv({ path: '.env.production', override: true });
}

import Koa from 'koa';

Expand Down Expand Up @@ -41,8 +46,7 @@ const server = app
console.log(
`Server "${process.env.SERVER_NAME}" started on localhost:${PORT}`,
),
)
;
);
server.requestTimeout = 0;
server.headersTimeout = 0;

Expand Down

0 comments on commit 79eaa05

Please sign in to comment.