Skip to content

Commit

Permalink
Merge branch 'main' into 62-use-bun-for-the-api
Browse files Browse the repository at this point in the history
  • Loading branch information
bnussman committed Oct 31, 2023
2 parents 415ddd2 + dcdd517 commit 699fdb2
Show file tree
Hide file tree
Showing 16 changed files with 609 additions and 428 deletions.
39 changes: 29 additions & 10 deletions api/migrations/.snapshot-beep.json
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 6,
"length": 0,
"mappedType": "datetime"
}
},
Expand Down Expand Up @@ -352,7 +352,7 @@
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"length": 0,
"mappedType": "datetime"
}
},
Expand Down Expand Up @@ -421,7 +421,7 @@
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"length": 0,
"mappedType": "datetime"
}
},
Expand Down Expand Up @@ -536,7 +536,7 @@
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"length": 0,
"mappedType": "datetime"
},
"updated": {
Expand All @@ -546,7 +546,7 @@
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"length": 0,
"mappedType": "datetime"
}
},
Expand Down Expand Up @@ -642,7 +642,7 @@
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"length": 0,
"mappedType": "datetime"
},
"end": {
Expand All @@ -652,7 +652,7 @@
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 6,
"length": 0,
"mappedType": "datetime"
},
"status": {
Expand All @@ -679,6 +679,25 @@
"name": "beep",
"schema": "public",
"indexes": [
{
"columnNames": [
"status"
],
"composite": false,
"keyName": "beep_status_index",
"primary": false,
"unique": false
},
{
"keyName": "beep_beeper_id_rider_id_index",
"columnNames": [
"beeper_id",
"rider_id"
],
"composite": true,
"primary": false,
"unique": false
},
{
"keyName": "beep_pkey",
"columnNames": [
Expand Down Expand Up @@ -780,7 +799,7 @@
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"length": 0,
"mappedType": "datetime"
},
"handled": {
Expand Down Expand Up @@ -924,7 +943,7 @@
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"length": 0,
"mappedType": "datetime"
},
"beep_id": {
Expand Down Expand Up @@ -1027,7 +1046,7 @@
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"length": 0,
"mappedType": "datetime"
},
"email": {
Expand Down
15 changes: 0 additions & 15 deletions api/migrations/Migration20230410000952.ts

This file was deleted.

13 changes: 13 additions & 0 deletions api/migrations/Migration20231027011333.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Migration } from '@mikro-orm/migrations';

export class Migration20231027011333 extends Migration {

async up(): Promise<void> {
this.addSql('create index "beep_status_index" on "beep" ("status");');
}

async down(): Promise<void> {
this.addSql('drop index "beep_status_index";');
}

}
13 changes: 13 additions & 0 deletions api/migrations/Migration20231027015047.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Migration } from '@mikro-orm/migrations';

export class Migration20231027015047 extends Migration {

async up(): Promise<void> {
this.addSql('create index "beep_beeper_id_rider_id_index" on "beep" ("beeper_id", "rider_id");');
}

async down(): Promise<void> {
this.addSql('drop index "beep_beeper_id_rider_id_index";');
}

}
34 changes: 17 additions & 17 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
"dependencies": {
"@apollo/server": "^4.9.4",
"@banksnussman/graphql-fields-to-relations": "^2.0.0",
"@mikro-orm/core": "5.8.8",
"@mikro-orm/postgresql": "5.8.8",
"@sentry/integrations": "^7.74.0",
"@sentry/node": "^7.74.0",
"aws-sdk": "^2.1473.0",
"@mikro-orm/core": "5.9.0",
"@mikro-orm/postgresql": "5.9.0",
"@sentry/integrations": "^7.75.1",
"@sentry/node": "^7.75.1",
"aws-sdk": "^2.1481.0",
"body-parser": "^1.20.2",
"car-info": "^0.1.5",
"class-validator": "^0.14.0",
Expand All @@ -40,27 +40,27 @@
"graphql-redis-subscriptions": "^2.6.0",
"graphql-subscriptions": "^2.0.0",
"graphql-upload-minimal": "^1.5.5",
"graphql-ws": "^5.14.1",
"graphql-ws": "^5.14.2",
"ioredis": "^5.3.2",
"js-sha256": "^0.10.1",
"nodemailer": "^6.9.6",
"nodemailer": "^6.9.7",
"reflect-metadata": "^0.1.13",
"type-graphql": "2.0.0-beta.2",
"uuid": "^9.0.1",
"wkx": "^0.5.0",
"ws": "^8.14.2"
},
"devDependencies": {
"@mikro-orm/cli": "5.8.8",
"@mikro-orm/migrations": "5.8.8",
"@types/body-parser": "^1.19.3",
"@types/cors": "^2.8.14",
"@types/express": "^4.17.19",
"@types/node": "^20.8.6",
"@types/nodemailer": "^6.4.11",
"@types/uuid": "^9.0.5",
"@types/ws": "^8.5.7",
"concurrently": "^8.2.1",
"@mikro-orm/cli": "5.9.0",
"@mikro-orm/migrations": "5.9.0",
"@types/body-parser": "^1.19.4",
"@types/cors": "^2.8.15",
"@types/express": "^4.17.20",
"@types/node": "^20.8.9",
"@types/nodemailer": "^6.4.13",
"@types/uuid": "^9.0.6",
"@types/ws": "^8.5.8",
"concurrently": "^8.2.2",
"ts-node": "^10.9.1",
"typescript": "^5.1.6",
"bun-types": "latest"
Expand Down
8 changes: 5 additions & 3 deletions api/src/entities/Beep.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Entity, Enum, Filter, ManyToOne, PrimaryKey, Property } from "@mikro-orm/core";
import { Entity, Enum, Filter, Index, ManyToOne, PrimaryKey, Property } from "@mikro-orm/core";
import { Field, ObjectType } from "type-graphql";
import { User } from "./User";
import { v4 } from 'uuid';
Expand Down Expand Up @@ -26,10 +26,11 @@ export enum Status {
$and: [
{ status: { $ne: Status.DENIED } },
{ status: { $ne: Status.COMPLETE } },
{ status: { $ne: Status.CANCELED } },
{ status: { $ne: Status.CANCELED } },
]
}
})
@Index({ properties: ["beeper", "rider"] })
export class Beep {
constructor(values?: Partial<Beep>) {
if (values) {
Expand Down Expand Up @@ -71,9 +72,10 @@ export class Beep {

@Field()
@Enum(() => Status)
@Index()
status: Status = Status.COMPLETE;

@Field()
@Property({ persist: false })
position: number = -1;
}
}
16 changes: 13 additions & 3 deletions api/src/rating/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,25 @@ export class RatingResolver {
}

@Mutation(() => Boolean)
@Authorized(UserRole.ADMIN)
@Authorized()
public async deleteRating(@Ctx() ctx: Context, @Arg('id') id: string): Promise<boolean> {
const rating = await ctx.em.findOneOrFail(Rating, id, { populate: ['rated'] });

if (!rating.rated.rating) throw new Error("You are trying to delete a rating for a user who's rating value is undefined");
if (ctx.user.role === UserRole.USER && rating.rater.id !== ctx.user.id) {
throw new Error("You can't delete a rating that you didn't create.");
}

if (!rating.rated.rating) {
throw new Error("You are trying to delete a rating for a user who's rating value is undefined");
}

const numberOfRatings = await ctx.em.count(Rating, { rated: rating.rated.id });

rating.rated.rating = numberOfRatings <= 1 ? undefined : rating.rated.rating = (rating.rated.rating * numberOfRatings - rating.stars) / (numberOfRatings - 1);
if (numberOfRatings <= 1) {
rating.rated.rating = undefined;
} else {
rating.rated.rating = (rating.rated.rating * numberOfRatings - rating.stars) / (numberOfRatings - 1);
}

ctx.em.persist(rating.rated);

Expand Down
49 changes: 44 additions & 5 deletions app/components/Rating.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import React from "react";
import { useNavigation } from "@react-navigation/native";
import { HStack, Spacer, Stack, Text } from "native-base";
import { GetRatingsQuery } from "../generated/graphql";
import { GetRatingsQuery, useDeleteRatingMutation } from "../generated/graphql";
import { Navigation } from "../utils/Navigation";
import { useUser } from "../utils/useUser";
import { Avatar } from "./Avatar";
import { printStars } from "./Stars";
import { Unpacked } from "../utils/constants";
import { Unpacked, isMobile } from "../utils/constants";
import { Card } from "./Card";
import { Alert } from "react-native";

type Rating = Unpacked<GetRatingsQuery["getRatings"]["items"]>;

interface Props {
item: Unpacked<GetRatingsQuery["getRatings"]["items"]>;
item: Rating;
index: number;
}

Expand All @@ -21,14 +24,50 @@ export function Rating(props: Props) {
const otherUser = user?.id === item.rater.id ? item.rated : item.rater;

const isRater = user?.id === item.rater.id;
const isRated = user?.id === item.rated.id;

const [deleteRating] = useDeleteRatingMutation({
variables: {
id: item.id
},
onError(error) {
alert(error.message);
},
update(cache) {
cache.evict({
id: cache.identify({
__typename: "Rating",
id: item.id,
}),
});
}
});

const onLongPress = () => {
if (isMobile) {
Alert.alert(
"Delete Rating?",
"Are you sure you want to delete this rating?",
[
{
text: "No",
style: "cancel",
},
{ text: "Yes", onPress: () => deleteRating() },
],
{ cancelable: true }
);
} else {
deleteRating();
}
};

return (
<Card
mt={2}
mx={1}
pressable
onPress={() => navigation.push("Profile", { id: otherUser.id })}
onLongPress={onLongPress}
>
<Stack space={2}>
<HStack alignItems="center" space={2}>
Expand All @@ -43,7 +82,7 @@ export function Rating(props: Props) {
{otherUser.name}
</Text>
<Text color="gray.400" fontSize="xs" isTruncated>
{`${isRater ? "Rated" : "Recieved"} - ${new Date(
{`${isRater ? "You rated" : "Rated you"} - ${new Date(
item.timestamp
).toLocaleString(undefined, { dateStyle: 'short', timeStyle: "short" })}`}
</Text>
Expand Down
Loading

0 comments on commit 699fdb2

Please sign in to comment.