-
Notifications
You must be signed in to change notification settings - Fork 0
SSF-225 FM Deleting & Editing Donation Item Backend #188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ import { FoodType } from './types'; | |
| import { Donation } from '../donations/donations.entity'; | ||
| import { CreateDonationItemDto } from './dtos/create-donation-items.dto'; | ||
| import { UpdateDonationItemDetailsDto } from './dtos/update-donation-item-details.dto'; | ||
| import { ReplaceDonationItemDto } from './dtos/replace-donation-item.dto'; | ||
|
|
||
| @Injectable() | ||
| export class DonationItemsService { | ||
|
|
@@ -131,6 +132,60 @@ export class DonationItemsService { | |
| return confirmedDetailsForAnItem; | ||
| } | ||
|
|
||
| async editItems( | ||
| donationId: number, | ||
| body: ReplaceDonationItemDto[], | ||
| transactionManager: EntityManager, | ||
| ): Promise<void> { | ||
| const itemRepo = transactionManager.getRepository(DonationItem); | ||
|
|
||
| const existingItems = await itemRepo.find({ where: { donationId } }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we only use the ids, so we should just be able to put the itemRepo.find and map into one call and then delete existingItems altogether, and just use existingIds on 163 |
||
| const existingIds = new Set(existingItems.map((item) => item.itemId)); | ||
|
|
||
| const providedIds = new Set<number>(); | ||
| for (const dto of body) { | ||
| if (dto.itemId === undefined) continue; | ||
|
|
||
| if (providedIds.has(dto.itemId)) { | ||
| throw new BadRequestException( | ||
| `Duplicate itemId ${dto.itemId} in request`, | ||
| ); | ||
| } | ||
| providedIds.add(dto.itemId); | ||
|
|
||
| if (!existingIds.has(dto.itemId)) { | ||
| throw new BadRequestException( | ||
| `Donation item ${dto.itemId} does not belong to Donation ${donationId}`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| const idsToDelete = existingItems | ||
| .map((item) => item.itemId) | ||
| .filter((id) => !providedIds.has(id)); | ||
|
|
||
| if (idsToDelete.length > 0) { | ||
| await itemRepo.delete({ itemId: In(idsToDelete) }); | ||
| } | ||
|
|
||
| const itemsToSave = body.map((dto) => | ||
| itemRepo.create({ | ||
| ...(dto.itemId !== undefined | ||
| ? { itemId: dto.itemId } | ||
| : { donationId, reservedQuantity: 0 }), | ||
| itemName: dto.itemName, | ||
| quantity: dto.quantity, | ||
| ozPerItem: dto.ozPerItem, | ||
| estimatedValue: dto.estimatedValue, | ||
| foodType: dto.foodType, | ||
| foodRescue: dto.foodRescue, | ||
| detailsConfirmed: true, | ||
| }), | ||
| ); | ||
|
|
||
| await itemRepo.save(itemsToSave); | ||
| } | ||
|
|
||
| async createMultiple( | ||
| savedDonation: Donation, | ||
| items: CreateDonationItemDto[], | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { | ||
| IsNumber, | ||
| IsString, | ||
| Min, | ||
| IsEnum, | ||
| IsNotEmpty, | ||
| Length, | ||
| IsOptional, | ||
| IsInt, | ||
| IsBoolean, | ||
| } from 'class-validator'; | ||
| import { FoodType } from '../types'; | ||
|
|
||
| // itemId present = update row, else add | ||
| export class ReplaceDonationItemDto { | ||
| @IsOptional() | ||
| @IsInt() | ||
| @Min(1) | ||
| itemId?: number; | ||
|
|
||
| @IsString() | ||
| @IsNotEmpty() | ||
| @Length(1, 255) | ||
| itemName!: string; | ||
|
|
||
| @IsInt() | ||
| @Min(1) | ||
| quantity!: number; | ||
|
|
||
| @IsNumber( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Yurika-Kan can you explain the rationale behind making these required? the case im considering: if i make a new donation with 2 items where i dont fill out the ozPerItem and estimatedValue, the next time i go to update the donation, i cant just make an edit to one of them, i have to update all fields for all values. i think they should be allowed to go in and just update the ozPerItem for one item if thats all they have. lmk if im missing something though!
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To my understanding we cannot now make new donation items without ozPerItem and estimatedValue?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They wanted to make all those fields now required upon creation so this was to keep consistency with that request
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ie oz, donation val, food rescue There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay, then in that case, while it wasnt directly in the ticket, i think we should make an update to the donationItems entity, and write a new migration to fix it in the database and fix the frontend create donations so everything is consistent again
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I asked Yurika specifically about the entity/db and she said it wasn't necessary at the time @Yurika-Kan thoughts? |
||
| { maxDecimalPlaces: 2 }, | ||
| { message: 'ozPerItem must have at most 2 decimal places' }, | ||
| ) | ||
| @Min(0.01) | ||
| ozPerItem!: number; | ||
|
|
||
| @IsNumber( | ||
| { maxDecimalPlaces: 2 }, | ||
| { message: 'estimatedValue must have at most 2 decimal places' }, | ||
| ) | ||
| @Min(0.01) | ||
| estimatedValue!: number; | ||
|
|
||
| @IsEnum(FoodType) | ||
| foodType!: FoodType; | ||
|
|
||
| @IsBoolean() | ||
| foodRescue!: boolean; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.