Skip to content

Commit

Permalink
Updated Donation schema
Browse files Browse the repository at this point in the history
  • Loading branch information
alay10 committed Apr 16, 2024
1 parent cad32ec commit c4009b6
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 38 deletions.
48 changes: 19 additions & 29 deletions scripts/createTestData.script
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if (true) {
db.donors.deleteMany({})
db.users.deleteMany({})
db.items.deleteMany({})
db.donationItem.deleteMany({})
db.donationItems.deleteMany({})
db.donations.deleteMany({})
}

Expand All @@ -23,7 +23,7 @@ db.donors.insertMany([
"address": "123 Main St",
"city": "Anytown",
"state": "CA",
"zip": "12345"
"zip": 12345
},
{
"_id": donorID2,
Expand All @@ -33,7 +33,7 @@ db.donors.insertMany([
"address": "456 Elm St",
"city": "Smallville",
"state": "NY",
"zip": "54321"
"zip": 54321
},
{
"_id": donorID3,
Expand All @@ -43,7 +43,7 @@ db.donors.insertMany([
"address": "789 Oak St",
"city": "Metropolis",
"state": "IL",
"zip": "67890"
"zip": 67890
},
{
"_id": donorID4,
Expand All @@ -53,7 +53,7 @@ db.donors.insertMany([
"address": "101 Pine St",
"city": "Gotham",
"state": "NJ",
"zip": "13579"
"zip": 13579
},
{
"_id": donorID5,
Expand All @@ -63,7 +63,7 @@ db.donors.insertMany([
"address": "246 Cedar St",
"city": "Central City",
"state": "MO",
"zip": "24680"
"zip": 24680
},
{
"_id": donorID6,
Expand All @@ -73,7 +73,7 @@ db.donors.insertMany([
"address": "369 Maple St",
"city": "Star City",
"state": "WA",
"zip": "97531"
"zip": 97531
},
{
"_id": donorID7,
Expand All @@ -83,7 +83,7 @@ db.donors.insertMany([
"address": "483 Birch St",
"city": "Atlantis",
"state": "CA",
"zip": "12345"
"zip": 12345
},
{
"_id": donorID8,
Expand All @@ -93,7 +93,7 @@ db.donors.insertMany([
"address": "579 Fir St",
"city": "Themyscira",
"state": "MA",
"zip": "54321"
"zip": 54321
},
{
"_id": donorID9,
Expand All @@ -103,7 +103,7 @@ db.donors.insertMany([
"address": "678 Pine St",
"city": "Xavier's School for Gifted Youngsters",
"state": "NY",
"zip": "67890"
"zip": 67890
},
{
"_id": donorID10,
Expand All @@ -113,7 +113,7 @@ db.donors.insertMany([
"address": "789 Elm St",
"city": "Hogwarts",
"state": "PA",
"zip": "13579"
"zip": 13579
},
])

Expand Down Expand Up @@ -278,7 +278,7 @@ for (let i = 1; i <= 10; i++) {
this[`donationItemId${i}`] = ObjectId();
}

db.donationItem.insertMany([
db.donationItems.insertMany([
{
"_id": donationItemId1,
"item": itemId1,
Expand All @@ -287,7 +287,6 @@ db.donationItem.insertMany([
"value": {
"price": 499.99,
"evaluation": "High",
"inRange": true
}
},
{
Expand All @@ -298,7 +297,6 @@ db.donationItem.insertMany([
"value": {
"price": 29.99,
"evaluation": "Low",
"inRange": true
}
},
{
Expand All @@ -308,8 +306,7 @@ db.donationItem.insertMany([
"barcode": "123XYZ",
"value": {
"price": 9.99,
"evaluation": "Other",
"inRange": false
"evaluation": "New",
}
},
{
Expand All @@ -320,7 +317,6 @@ db.donationItem.insertMany([
"value": {
"price": 29.99,
"evaluation": "High",
"inRange": true
}
},
{
Expand All @@ -331,7 +327,6 @@ db.donationItem.insertMany([
"value": {
"price": 24.99,
"evaluation": "Low",
"inRange": true
}
},
{
Expand All @@ -341,8 +336,7 @@ db.donationItem.insertMany([
"barcode": "XYZ123",
"value": {
"price": 599.99,
"evaluation": "Other",
"inRange": false
"evaluation": "New",
}
},
{
Expand All @@ -353,7 +347,6 @@ db.donationItem.insertMany([
"value": {
"price": 129.99,
"evaluation": "High",
"inRange": true
}
},
{
Expand All @@ -364,7 +357,6 @@ db.donationItem.insertMany([
"value": {
"price": 34.99,
"evaluation": "Low",
"inRange": true
}
},
{
Expand All @@ -374,8 +366,7 @@ db.donationItem.insertMany([
"barcode": "123ABC",
"value": {
"price": 599.99,
"evaluation": "Other",
"inRange": false
"evaluation": "New",
}
},
{
Expand All @@ -386,7 +377,6 @@ db.donationItem.insertMany([
"value": {
"price": 199.99,
"evaluation": "High",
"inRange": true
}
}
])
Expand All @@ -401,24 +391,24 @@ db.donations.insertMany([
"user": userId1,
"items": [itemId1],
"entryDate": "2024-01-31",
"donor": donorId1
"donor": donorID1
},
{
"user": userId1,
"items": [itemId2, itemId4],
"entryDate": "2024-02-01",
"donor": donorId2
"donor": donorID2
},
{
"user": userId2,
"items": [itemId4, itemId5, itemId6],
"entryDate": "2024-02-02",
"donor": donorId3
"donor": donorID3
},
{
"user": userId3,
"items": [itemId7, itemId8, itemId9, itemId10],
"entryDate": "2024-02-03",
"donor": donorId4
"donor": donorID4
}
])
3 changes: 2 additions & 1 deletion src/app/api/donations/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export async function GET() {
try {
const result = await getAllDonations();
return NextResponse.json(result, { status: 200 });
} catch {
} catch (error) {
console.log(error);
return NextResponse.json({ message: 'Unknown Error' }, { status: 500 });
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/server/actions/donations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import {
DonationEntity,
DonationResponse,
} from '@/types/donation';
import UserSchema from '@/server/models/users';
import DonorSchema from '@/server/models/donors';
UserSchema;
DonorSchema;

export async function createDonation(
donation: CreateDonationRequest
Expand All @@ -19,7 +23,11 @@ export async function getAllDonations(): Promise<DonationResponse[]> {
try {
await dbConnect();

const response: DonationResponse[] = await DonationSchema.find();
console.log('Donation Call');
const response: DonationResponse[] = await DonationSchema.find().populate([
'user',
'donor',
]);

return response;
} catch (error) {
Expand Down
4 changes: 0 additions & 4 deletions src/server/models/donationItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ const DonationItemSchema = new Schema(
enum: evaluationEnum,
required: false,
},
inRange: {
type: Boolean,
required: true,
},
},
required: true,
},
Expand Down
3 changes: 1 addition & 2 deletions src/types/donation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { z } from 'zod';
import zObjectId from './objectId';
import { zItemResponse } from './items';

export const evaluationEnum = ['Hight', 'Low', 'Other'] as const;
export const evaluationEnum = ['Hight', 'Low', 'New'] as const;

export const zDonationItemBase = z.object({
item: zObjectId,
Expand All @@ -12,7 +12,6 @@ export const zDonationItemBase = z.object({
value: z.object({
price: z.number(),
evaluation: z.enum(evaluationEnum),
inRange: z.boolean(),
}),
});

Expand Down
2 changes: 1 addition & 1 deletion test/EXdonor.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"zip": {
"$id": "#root/zip",
"title": "Zip",
"type": "string"
"type": "number"
}
}
}

0 comments on commit c4009b6

Please sign in to comment.