Skip to content

Commit

Permalink
Merge pull request #315 from labzero/jeffrey/team-api
Browse files Browse the repository at this point in the history
Convert rest of team API to TypeScript
  • Loading branch information
JeffreyATW authored May 20, 2023
2 parents 99bd24e + 69e8867 commit ce26f46
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 46 deletions.
2 changes: 1 addition & 1 deletion global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ declare global {
subdomain?: string;
team?: Team;
user?: UserInterface;
wss?: Server;
wss?: WebSocket.Server;
}
export interface User extends UserInterface {}
}
Expand Down
2 changes: 1 addition & 1 deletion src/actions/restaurants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function renameRestaurant(id: number, obj: Partial<Restaurant>): Action {

export function restaurantRenamed(
id: number,
obj: Restaurant,
obj: Partial<Restaurant>,
userId: number
): Action {
return {
Expand Down
20 changes: 10 additions & 10 deletions src/api/team/restaurantTags.js → src/api/team/restaurantTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "../../actions/restaurants";

export default () => {
const router = new Router({ mergeParams: true });
const router = Router({ mergeParams: true });

return router
.post("/", loggedIn, checkTeamRole(), async (req, res, next) => {
Expand All @@ -24,7 +24,7 @@ export default () => {
Tag.findOrCreate({
where: {
name: req.body.name.toLowerCase().trim(),
teamId: req.team.id,
teamId: req.team!.id,
},
})
.then(async ([tag]) => {
Expand All @@ -36,12 +36,12 @@ export default () => {
const json = tag.toJSON();
json.restaurant_count = 1;
req.broadcast(
req.team.id,
postedNewTagToRestaurant(restaurantId, json, req.user.id)
req.team!.id,
postedNewTagToRestaurant(restaurantId, json, req.user!.id)
);
res.status(201).send({ error: false, data: json });
} catch (err) {
alreadyAddedError(err);
alreadyAddedError();
}
})
.catch((err) => {
Expand All @@ -57,12 +57,12 @@ export default () => {

const json = obj.toJSON();
req.broadcast(
req.team.id,
postedTagToRestaurant(restaurantId, id, req.user.id)
req.team!.id,
postedTagToRestaurant(restaurantId, id, req.user!.id)
);
res.status(201).send({ error: false, data: json });
} catch (err) {
alreadyAddedError(err);
alreadyAddedError();
}
} else {
next();
Expand All @@ -74,8 +74,8 @@ export default () => {
try {
await RestaurantTag.destroy({ where: { restaurantId, tagId: id } });
req.broadcast(
req.team.id,
deletedTagFromRestaurant(restaurantId, id, req.user.id)
req.team!.id,
deletedTagFromRestaurant(restaurantId, id, req.user!.id)
);
res.status(204).send();
} catch (err) {
Expand Down
31 changes: 18 additions & 13 deletions src/api/team/restaurants.js → src/api/team/restaurants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Router } from "express";
import { Response, Router } from "express";
import fetch from "node-fetch";
import { Restaurant, Vote, Tag } from "../../db";
import { Restaurant as RestaurantInterface } from "../../interfaces";
import checkTeamRole from "../helpers/checkTeamRole";
import loggedIn from "../helpers/loggedIn";
import {
Expand All @@ -12,10 +13,10 @@ import voteApi from "./votes";
import restaurantTagApi from "./restaurantTags";

export default () => {
const router = new Router({ mergeParams: true });
const router = Router({ mergeParams: true });
const apikey = process.env.GOOGLE_SERVER_APIKEY;

const notFound = (res) => {
const notFound = (res: Response) => {
res
.status(404)
.json({ error: true, data: { message: "Restaurant not found." } });
Expand All @@ -24,7 +25,9 @@ export default () => {
return router
.get("/", loggedIn, checkTeamRole(), async (req, res, next) => {
try {
const all = await Restaurant.findAllWithTagIds({ teamId: req.team.id });
const all = await Restaurant.findAllWithTagIds({
teamId: req.team!.id,
});

res.status(200).json({ error: false, data: all });
} catch (err) {
Expand All @@ -39,7 +42,7 @@ export default () => {
try {
const r = await Restaurant.findByPk(parseInt(req.params.id, 10));

if (r === null || r.teamId !== req.team.id) {
if (r === null || r.teamId !== req.team!.id) {
notFound(res);
} else {
const response = await fetch(
Expand Down Expand Up @@ -83,7 +86,7 @@ removed its entry. Try removing it and adding it to Lunch again.`,
address,
lat,
lng,
teamId: req.team.id,
teamId: req.team!.id,
votes: [],
tags: [],
},
Expand All @@ -93,7 +96,7 @@ removed its entry. Try removing it and adding it to Lunch again.`,
const json = obj.toJSON();
json.all_decision_count = 0;
json.all_vote_count = 0;
req.broadcast(req.team.id, restaurantPosted(json, req.user.id));
req.broadcast(req.team!.id, restaurantPosted(json, req.user!.id));
res.status(201).send({ error: false, data: json });
} catch (err) {
const error = {
Expand All @@ -110,18 +113,20 @@ removed its entry. Try removing it and adding it to Lunch again.`,
{ name },
{
fields: ["name"],
where: { id, teamId: req.team.id },
where: { id, teamId: req.team!.id },
returning: true,
}
)
.then(([count, rows]) => {
if (count === 0) {
notFound(res);
} else {
const json = { name: rows[0].toJSON().name };
const json: Partial<RestaurantInterface> = {
name: rows[0].toJSON().name,
};
req.broadcast(
req.team.id,
restaurantRenamed(id, json, req.user.id)
req.team!.id,
restaurantRenamed(id, json, req.user!.id)
);
res.status(200).send({ error: false, data: json });
}
Expand All @@ -135,12 +140,12 @@ removed its entry. Try removing it and adding it to Lunch again.`,
const id = parseInt(req.params.id, 10);
try {
const count = await Restaurant.destroy({
where: { id, teamId: req.team.id },
where: { id, teamId: req.team!.id },
});
if (count === 0) {
notFound(res);
} else {
req.broadcast(req.team.id, restaurantDeleted(id, req.user.id));
req.broadcast(req.team!.id, restaurantDeleted(id, req.user!.id));
res.status(204).send();
}
} catch (err) {
Expand Down
14 changes: 6 additions & 8 deletions src/api/team/tags.js → src/api/team/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import loggedIn from "../helpers/loggedIn";
import { tagDeleted } from "../../actions/tags";

export default () => {
const router = new Router({ mergeParams: true });
const router = Router({ mergeParams: true });

return router
.get("/", loggedIn, checkTeamRole(), async (req, res, next) => {
try {
const all = await Tag.scope("orderedByRestaurant").findAll({
distinct: true,
where: { teamId: req.team.id },
where: { teamId: req.team!.id },
});
res.status(200).send({ error: false, data: all });
} catch (err) {
Expand All @@ -22,16 +21,15 @@ export default () => {
.delete("/:id", loggedIn, checkTeamRole(), async (req, res, next) => {
const id = parseInt(req.params.id, 10);
try {
const count = await Tag.destroy({ where: { id, teamId: req.team.id } });
const count = await Tag.destroy({
where: { id, teamId: req.team!.id },
});
if (count === 0) {
res
.status(404)
.json({ error: true, data: { message: "Tag not found." } });
} else {
req.broadcast(
req.team.id,
tagDeleted(id, req.user.id, req.team.slug)
);
req.broadcast(req.team!.id, tagDeleted(id, req.user!.id));
res.status(204).send();
}
} catch (err) {
Expand Down
18 changes: 9 additions & 9 deletions src/api/team/votes.js → src/api/team/votes.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Router } from "express";
import { Response, Router } from "express";
import { sequelize, Vote } from "../../db";
import checkTeamRole from "../helpers/checkTeamRole";
import loggedIn from "../helpers/loggedIn";
import { votePosted, voteDeleted } from "../../actions/restaurants";

export default () => {
const router = new Router({ mergeParams: true });
const router = Router({ mergeParams: true });

const notFound = (res) => {
const notFound = (res: Response) => {
res.status(404).json({ error: true, data: { message: "Vote not found." } });
};

Expand All @@ -18,15 +18,15 @@ export default () => {
const result = await sequelize.transaction(async (t) => {
const count = await Vote.recentForRestaurantAndUser(
restaurantId,
req.user.id,
req.user!.id,
t
);

if (count === 0) {
return Vote.create(
{
restaurantId,
userId: req.user.id,
userId: req.user!.id,
},
{ transaction: t }
);
Expand All @@ -41,7 +41,7 @@ export default () => {
} else {
try {
const json = result.toJSON();
req.broadcast(req.team.id, votePosted(json));
req.broadcast(req.team!.id, votePosted(json));
res.status(201).send({ error: false, data: result });
} catch (err) {
next(err);
Expand All @@ -56,15 +56,15 @@ export default () => {

try {
const count = await Vote.destroy({
where: { id, userId: req.user.id },
where: { id, userId: req.user!.id },
});

if (count === 0) {
notFound(res);
} else {
req.broadcast(
req.team.id,
voteDeleted(parseInt(req.params.restaurantId, 10), req.user.id, id)
req.team!.id,
voteDeleted(parseInt(req.params.restaurantId, 10), req.user!.id, id)
);
res.status(204).send();
}
Expand Down
4 changes: 1 addition & 3 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ export type Action =
| {
type: "RESTAURANT_RENAMED";
id: number;
fields: {
name: string;
};
fields: Partial<Restaurant>;
userId: number;
}
| {
Expand Down
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"skipLibCheck": true,
"strict": true,
"sourceMap": true,
"noImplicitAny": true,
"module": "esnext",
"target": "es6",
"jsx": "react",
Expand Down

0 comments on commit ce26f46

Please sign in to comment.