Skip to content

Commit

Permalink
link fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dowhep committed Oct 11, 2023
1 parent 89132a7 commit 8336686
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 14 deletions.
8 changes: 6 additions & 2 deletions api/controllers/MerchStoreController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,12 @@ export class MerchStoreController {

// generate a random string for the uploaded photo url
const uniqueFileName = uuid();
const uploadedPhoto = await this.storageService.uploadToFolder(file, MediaType.MERCH_PHOTO, uniqueFileName, params.uuid);
const merchPhoto = await this.merchStoreService.createItemPhoto(params.uuid, { uploadedPhoto, position: createItemPhotoRequest.position });
const uploadedPhoto = await this.storageService.uploadToFolder(
file, MediaType.MERCH_PHOTO, uniqueFileName, params.uuid,
);
const merchPhoto = await this.merchStoreService.createItemPhoto(
params.uuid, { uploadedPhoto, position: createItemPhotoRequest.position },
);

return { error: null, merchPhoto };
}
Expand Down
6 changes: 3 additions & 3 deletions migrations/0037-add-merch-item-image-table.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MigrationInterface, QueryRunner, Table, TableColumn, TableIndex } from 'typeorm';
import { MigrationInterface, QueryRunner, Table, TableColumn } from 'typeorm';

const TABLE_NAME = 'MerchandiseItemPhotos';
const MERCH_TABLE_NAME = 'MerchandiseItems';
Expand Down Expand Up @@ -39,7 +39,7 @@ export class AddMerchItemImageTable1681777109787 implements MigrationInterface {
{
name: 'images_by_item_index',
columnNames: ['merchItem'],
}
},
],
// cascade delete
foreignKeys: [
Expand All @@ -49,7 +49,7 @@ export class AddMerchItemImageTable1681777109787 implements MigrationInterface {
referencedColumnNames: ['uuid'],
onDelete: 'CASCADE',
},
]
],
}));

// add images from each item of the merchandise table to the photo table
Expand Down
4 changes: 3 additions & 1 deletion models/MerchandiseItemPhotoModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export class MerchandiseItemPhotoModel extends BaseEntity {
@PrimaryGeneratedColumn('uuid')
uuid: Uuid;

@ManyToOne((type) => MerchandiseItemModel, (merchItem) => merchItem.merchPhotos, { nullable: false, onDelete: 'CASCADE' })
@ManyToOne((type) => MerchandiseItemModel,
(merchItem) => merchItem.merchPhotos,
{ nullable: false, onDelete: 'CASCADE' })
@JoinColumn({ name: 'merchItem' })
@Index('images_by_item_index')
merchItem: MerchandiseItemModel;
Expand Down
7 changes: 4 additions & 3 deletions services/MerchStoreService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { EntityManager } from 'typeorm';
import { difference, flatten, intersection } from 'underscore';
import * as moment from 'moment-timezone';
import { MerchItemWithQuantity, OrderItemPriceAndQuantity } from 'types/internal';
import { Config } from '../config';
import { MerchandiseItemOptionModel } from '../models/MerchandiseItemOptionModel';
import {
Uuid,
Expand Down Expand Up @@ -220,7 +219,9 @@ export default class MerchStoreService {
// error on duplicate photo uuids
const dupSet = new Set();
merchPhotos.forEach((merchPhoto) => {
if (dupSet.has(merchPhoto.uuid)) { throw new UserError(`Multiple edits is made to photo: ${merchPhoto.uuid}`); }
if (dupSet.has(merchPhoto.uuid)) {
throw new UserError(`Multiple edits is made to photo: ${merchPhoto.uuid}`);
}
dupSet.add(merchPhoto.uuid);
});

Expand Down Expand Up @@ -346,7 +347,7 @@ export default class MerchStoreService {
* @returns the photo object removed from database
*/
public async deleteItemPhoto(uuid: Uuid): Promise<MerchItemPhoto> {
return await this.transactions.readWrite(async (txn) => {
return this.transactions.readWrite(async (txn) => {
const merchStoreItemPhotoRepository = Repositories.merchStoreItemPhoto(txn);
const merchPhoto = await merchStoreItemPhotoRepository.findByUuid(uuid);
if (!merchPhoto) throw new NotFoundError('Merch item photo not found');
Expand Down
2 changes: 1 addition & 1 deletion tests/data/MerchFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export class MerchFactory {
private static createPhotos(n: number): MerchandiseItemPhotoModel[] {
return FactoryUtils
.create(n, () => MerchFactory.fakePhoto())
.map((merchPhoto, i) => MerchandiseItemPhotoModel.merge(merchPhoto, { position: i }));;
.map((merchPhoto, i) => MerchandiseItemPhotoModel.merge(merchPhoto, { position: i }));
}

private static randomPrice(): number {
Expand Down
6 changes: 2 additions & 4 deletions tests/merchStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -888,14 +888,13 @@ describe('merch item photos', () => {
const merchStoreController = ControllerFactory.merchStore(
conn,
undefined,
instance(storageService)
instance(storageService),
);
const params = { uuid: item.uuid };

// verify before deleting, the photos all exist
const itemInDatabase = (await merchStoreController.getOneMerchItem(params, admin)).item
const itemInDatabase = (await merchStoreController.getOneMerchItem(params, admin)).item;
expect(itemInDatabase.merchPhotos).toEqual(merchPhotos);
console.log(itemInDatabase);

const deleteMerchItemPhotoParam1 = { uuid: photo1.uuid };
const deleteMerchItemPhotoParam2 = { uuid: photo2.uuid };
Expand All @@ -911,7 +910,6 @@ describe('merch item photos', () => {
expect(newPhotos[0].uuid).toEqual(photo2.uuid);
expect(newPhotos[0].position).toEqual(1);


// verify visible item photo limitation
expect(merchStoreController.deleteMerchItemPhoto(deleteMerchItemPhotoParam2, admin))
.rejects.toThrow('Cannot delete the only photo for a visible merch item');
Expand Down

0 comments on commit 8336686

Please sign in to comment.