Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
riyap committed Apr 24, 2024
2 parents a4d7a56 + e4072a8 commit ba7d327
Show file tree
Hide file tree
Showing 16 changed files with 2,062 additions and 27 deletions.
31 changes: 31 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module.exports = {
env: {
browser: true,
es2021: true,
},
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier",
],
overrides: [
{
env: {
node: true,
},
files: [".eslintrc.{js,cjs}"],
parserOptions: {
sourceType: "script",
},
},
],
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: "latest",
sourceType: "module",
},
plugins: ["@typescript-eslint", "prettier"],
rules: {
"prettier/prettier": ["error"],
},
};
24 changes: 24 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Lint on Push and PR

on:
push:
branches:
- 'main'
- 'dev**'
pull_request:
branches:
- 'main'

jobs:
lint:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '18.x'
- name: Install dependencies
run: yarn
- name: Run ESLint
run: npx eslint .
22 changes: 22 additions & 0 deletions appspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: 0.0
os: linux
files:
- source: /
destination: /home/ubuntu/rp-api
file_exists_behavior: OVERWRITE
hooks:
BeforeInstall:
- location: scripts/install_dependencies.sh
timeout: 300
runas: root
AfterInstall:
- location: scripts/build.sh
timeout: 300
runas: root
ApplicationStart:
- location: scripts/install_dependencies.sh
timeout: 300
runas: root
- location: scripts/reload_server.sh
timeout: 300
runas: root
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,18 @@
"@types/express": "^4.17.21",
"@types/morgan": "^1.9.9",
"@types/node": "^20.9.3",
"@types/uuid": "^9.0.8",
"@typescript-eslint/eslint-plugin": "^6.4.0",
"eslint": "8.2.0",
"eslint-config-airbnb": "19.0.4",
"eslint-config-prettier": "^9.1.0",
"eslint-config-standard-with-typescript": "^43.0.1",
"eslint-plugin-import": "2.25.3",
"eslint-plugin-jsx-a11y": "6.5.1",
"eslint-plugin-n": "^15.0.0 || ^16.0.0 ",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-promise": "^6.0.0",
"eslint-plugin-react": "7.28.0",
"eslint-plugin-react-hooks": "4.3.0",
"nodemon": "^3.0.1",
"prettier": "3.2.5"
},
Expand Down
3 changes: 3 additions & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
cd /home/ubuntu/rp-api
tsc
3 changes: 3 additions & 0 deletions scripts/install_dependencies.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
cd /home/ubuntu/rp-api
yarn
3 changes: 3 additions & 0 deletions scripts/reload_server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
cd /home/ubuntu/rp-api
pm2 reload RP_API
4 changes: 4 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import errorHandler from "./middleware/error-handler";

import authRouter from "./services/auth/auth-router";
import eventRouter from "./services/events/events-router";
import subscriptionRouter from "./services/subscription/subscription-router";

const app = express();

Expand All @@ -21,8 +22,10 @@ app.use("/", morgan("dev"));

app.use("/", bodyParser.json());

// API routes
app.use("/auth", authRouter);
app.use("/event", eventRouter);
app.use("/subscription", subscriptionRouter);

app.get("/status", (_, res) => {
console.log(StatusCodes.OK);
Expand All @@ -37,5 +40,6 @@ app.use(errorHandler);

app.listen(Config.DEFAULT_APP_PORT, async () => {
await connectToDatabase();
process.send?.("ready");
console.log("Server is listening on port 3000...");
});
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { getEnv } from "./utilities";

export const Environment = z.enum(["PRODUCTION", "DEVELOPMENT", "TESTING"]);

export const MailingListName = z.enum(["rp_interest"]);

export const Config = {
DEFAULT_APP_PORT: 3000,
ENV: Environment.parse(getEnv("ENV")),
Expand Down
9 changes: 9 additions & 0 deletions src/database.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import mongoose, { Schema } from "mongoose";
import { RoleInfo, RoleSchema } from "./services/auth/auth-schema";
import { EventSchema, EventValidator } from "./services/events/events-schema";
import {
SubscriptionValidator,
SubscriptionSchema,
} from "./services/subscription/subscription-schema";

mongoose.set("toObject", { versionKey: false });

Expand Down Expand Up @@ -34,4 +38,9 @@ function initializeModel(
export const Database = {
ROLES: initializeModel("roles", RoleSchema, RoleInfo),
EVENTS: initializeModel("events", EventSchema, EventValidator),
SUBSCRIPTION: initializeModel(
"subscription",
SubscriptionSchema,
SubscriptionValidator
),
};
8 changes: 4 additions & 4 deletions src/middleware/error-handler.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { NextFunction, Request, Response } from "express";

import { Request, Response } from "express";
// NextFunction
// TODO: Fix this function
function errorHandler(
err: Error,
_req: Request,
res: Response,
next: NextFunction
res: Response
// _next: NextFunction
) {
console.error("IN HERE", err.stack);
return res.status(500).send("Something broke!");
Expand Down
1 change: 0 additions & 1 deletion src/services/events/events-schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// import mongoose from "mongoose";
import { Schema } from "mongoose";
import { z } from "zod";
import { v4 as uuidv4 } from "uuid";
Expand Down
23 changes: 23 additions & 0 deletions src/services/subscription/subscription-router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Router } from "express";
import { StatusCodes } from "http-status-codes";
import { SubscriptionValidator } from "./subscription-schema";
import { Database } from "../../database";

const subscriptionRouter = Router();

// Create a new subscription
subscriptionRouter.post("/", async (req, res, next) => {
try {
const subscriptionData = SubscriptionValidator.parse(req.body);
await Database.SUBSCRIPTION.findOneAndUpdate(
{ mailingList: subscriptionData.mailingList },
{ $push: { subscriptions: subscriptionData.email } },
{ upsert: true, new: true }
);
return res.status(StatusCodes.CREATED).json(subscriptionData);
} catch (error) {
next(error);
}
});

export default subscriptionRouter;
17 changes: 17 additions & 0 deletions src/services/subscription/subscription-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import mongoose from "mongoose";
import { z } from "zod";
import { MailingListName } from "../../config";

// Zod schema for subscription
const SubscriptionValidator = z.object({
email: z.string().email(),
mailingList: MailingListName,
});

// Mongoose schema for subscription
const SubscriptionSchema = new mongoose.Schema({
email: { type: String, required: true },
mailingList: { type: String, required: true },
});

export { SubscriptionValidator, SubscriptionSchema };
2 changes: 1 addition & 1 deletion src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function getDatabaseUrl() {
const host = Config.DATABASE_HOST;
let database;
if (isProd()) {
return "";
database = `prod`;
} else if (isDev()) {
database = `dev-${username}`;
}
Expand Down
Loading

0 comments on commit ba7d327

Please sign in to comment.