Skip to content

Commit

Permalink
Lint pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
ardaozdere committed Oct 10, 2023
1 parent ea7dc7a commit 71db953
Show file tree
Hide file tree
Showing 21 changed files with 42 additions and 39 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ jobs:
- name: Run unit tests
run: yarn test:unit

- name: Run lint
run: yarn lint

cloud_build:
needs: build_and_test
runs-on: ubuntu-latest
Expand Down
4 changes: 2 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ app.use(
validateRequests: true,
// also validate our responses to the clients
validateResponses: true,
})
}),
);

// Instantiate dependencies and pass them to the respective components needed for our use cases
const budgetUseCases = BudgetService(
BudgetSummaryMongoRepository(),
BudgetMongoRepository()
BudgetMongoRepository(),
);
const expenseUseCases = ExpenseService(ExpenseMongoRepository());
app.use(ApiRouter(budgetUseCases, expenseUseCases));
Expand Down
2 changes: 1 addition & 1 deletion src/config/mongoDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ if (environment.LOG_LEVEL === "debug") {
};
logger.debug(
`\x1B[0;36mMongoose:\x1B[0m: ${collectionName}.${methodName}` +
`(${methodArgs.map(msgMapper).join(", ")})`
`(${methodArgs.map(msgMapper).join(", ")})`,
);
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/domain/limit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Limit {
if (amount < 0) {
throw new AppError(
"InvalidLimit",
`Limits cannot be negative: ${amount}`
`Limits cannot be negative: ${amount}`,
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/logging/expressLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const expressLogger = expressWinston.logger({
transports: [new winston.transports.Console()],
format: winston.format.combine(
winston.format.colorize(),
winston.format.simple()
winston.format.simple(),
),
meta: true,
expressFormat: true,
Expand Down
4 changes: 2 additions & 2 deletions src/logging/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ logger.add(
new winston.transports.Console({
format: winston.format.combine(
winston.format.colorize({ all: true }),
winston.format.simple()
winston.format.simple(),
),
})
}),
);

/*
Expand Down
14 changes: 7 additions & 7 deletions src/middleware/errorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class AppError extends Error {
public message: string,
public HTTPStatus: number = 500,
public isTrusted = true,
public cause?: unknown
public cause?: unknown,
) {
super(message);
}
Expand All @@ -34,14 +34,14 @@ export const listenToErrorEvents = (httpServer: Http.Server): void => {

process.on("SIGTERM", () => {
logger.error(
"App received SIGTERM event, try to gracefully close the server"
"App received SIGTERM event, try to gracefully close the server",
);
terminateHttpServerAndExit();
});

process.on("SIGINT", () => {
logger.error(
"App received SIGINT event, try to gracefully close the server"
"App received SIGINT event, try to gracefully close the server",
);
terminateHttpServerAndExit();
});
Expand All @@ -58,7 +58,7 @@ const handleError = (errorToHandle: unknown): void => {
} catch (handlingError: unknown) {
// Not using the logger here because it might have failed
process.stdout.write(
"The error handler failed, here are the handler failure and then the origin error that it tried to handle"
"The error handler failed, here are the handler failure and then the origin error that it tried to handle",
);
process.stdout.write(JSON.stringify(handlingError));
process.stdout.write(JSON.stringify(errorToHandle));
Expand Down Expand Up @@ -86,16 +86,16 @@ const normalizeError = (errorToHandle: unknown): AppError => {
return new AppError(
"general-error",
`Error Handler received a none error instance with type - ${inputType}, value - ${util.inspect(
errorToHandle
)}`
errorToHandle,
)}`,
);
};

export const errorHandler = (
err: Error,
_: Request,
res: Response,
next: NextFunction
next: NextFunction,
): void => {
if (typeof err === "object") {
const error = err as AppError;
Expand Down
2 changes: 1 addition & 1 deletion src/preStart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const args = parse<Args>(
alias: "e",
},
},
{ partial: true }
{ partial: true },
);

// Set the env file
Expand Down
4 changes: 2 additions & 2 deletions src/repository/budget/mongo/budgetSummaryMongoRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { type BudgetId } from "../../../domain/budget";
import { BudgetSummaryModel } from "./models";

export const insertBudgetSummary: BudgetSummaryRepository["insert"] = async (
summary
summary,
) => {
const summaryEntity = BudgetSummaryEntityConverter.toEntity(summary);
const summaryModel = new BudgetSummaryModel(summaryEntity);
Expand All @@ -13,7 +13,7 @@ export const insertBudgetSummary: BudgetSummaryRepository["insert"] = async (
};

export const findBudgetSummary: BudgetSummaryRepository["find"] = async (
budgetId: BudgetId
budgetId: BudgetId,
) => {
const found = await BudgetSummaryModel.findOne({ id: budgetId.value });
if (found != null) {
Expand Down
2 changes: 1 addition & 1 deletion src/repository/budget/mongo/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import type BudgetSummaryEntity from "../entity/budgetSummaryEntity";
export const BudgetSummaryModel = mongoose.model<BudgetSummaryEntity>(
"BudgetSummary",
budgetSummarySchema,
"budget-summaries"
"budget-summaries",
);
2 changes: 1 addition & 1 deletion src/repository/budget/mongo/schema/budgetSummarySchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const budgetSummarySchema = new mongoose.Schema<BudgetSummaryEntity>(
endDate: Types.Date,
expenses: [expenseSchema],
},
{ strict: true, timestamps: true, versionKey: false }
{ strict: true, timestamps: true, versionKey: false },
);

export default budgetSummarySchema;
2 changes: 1 addition & 1 deletion src/repository/expense/expenseRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface ExpenseRepository {
insert: (
budgetId: BudgetId,
spent: number,
expense: Expense
expense: Expense,
) => Promise<Expense | undefined>;
}

Expand Down
6 changes: 3 additions & 3 deletions src/repository/expense/mongo/ExpenseMongoRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const findAllExpensesForBudget: ExpenseRepository["findAllForBudget"] =
export const insertExpense: ExpenseRepository["insert"] = async (
budgetId,
spent,
expense
expense,
) => {
const expenseEntity = ExpenseEntityConverter.toEntity(expense);
const updatedSummary = await BudgetSummaryModel.findOneAndUpdate(
Expand All @@ -24,10 +24,10 @@ export const insertExpense: ExpenseRepository["insert"] = async (
$set: { spent },
$push: { expenses: expenseEntity },
},
{ returnDocument: "after" }
{ returnDocument: "after" },
);
const updatedExpense = updatedSummary?.expenses.find(
(t) => t.id === expense.id.value
(t) => t.id === expense.id.value,
);
if (updatedExpense !== undefined) {
return ExpenseEntityConverter.toDomain(updatedExpense);
Expand Down
2 changes: 1 addition & 1 deletion src/repository/expense/mongo/schema/expenseSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const expenseSchema = new mongoose.Schema<ExpenseEntity>(
amount: Types.Number,
date: Types.Date,
},
{ strict: true, _id: false }
{ strict: true, _id: false },
);

export default expenseSchema;
2 changes: 1 addition & 1 deletion src/routes/apiRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type ExpenseUseCases from "../usecase/expense/expenseUseCases";
// Express router bundling all individual routes of our app
const ApiRouter = (
budgetUseCases: BudgetUseCases,
expenseUseCases: ExpenseUseCases
expenseUseCases: ExpenseUseCases,
): Router => {
const router = Router();
router.use(BudgetRouter(budgetUseCases));
Expand Down
10 changes: 5 additions & 5 deletions src/routes/budget/budgetRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,27 @@ export const deleteBudget =
const budgetId = new UUID(req.params.budgetId);
const deleteResult = await deleteBudget(budgetId);
res.sendStatus(
deleteResult.deleted ? StatusCodes.NO_CONTENT : StatusCodes.NOT_FOUND
deleteResult.deleted ? StatusCodes.NO_CONTENT : StatusCodes.NOT_FOUND,
);
};

const BudgetRouter = (budgetUseCases: BudgetUseCases): Router => {
const router = Router();
router.post(
toExpressPath(apiPaths.createBudget),
asyncHandler(createBudget(budgetUseCases.createBudget))
asyncHandler(createBudget(budgetUseCases.createBudget)),
);
router.get(
toExpressPath(apiPaths.getBudgets),
asyncHandler(getBudgets(budgetUseCases.getBudgets))
asyncHandler(getBudgets(budgetUseCases.getBudgets)),
);
router.get(
toExpressPath(apiPaths.getBudgetSummary),
asyncHandler(getBudgetSummary(budgetUseCases.getBudgetSummary))
asyncHandler(getBudgetSummary(budgetUseCases.getBudgetSummary)),
);
router.delete(
toExpressPath(apiPaths.deleteBudget),
asyncHandler(deleteBudget(budgetUseCases.deleteBudget))
asyncHandler(deleteBudget(budgetUseCases.deleteBudget)),
);

return router;
Expand Down
2 changes: 1 addition & 1 deletion src/routes/expense/expenseRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const ExpenseRouter = (expenseUseCases: ExpenseUseCases): Router => {
const router = Router();
router.post(
toExpressPath(apiPaths.createExpense),
asyncHandler(createExpense(expenseUseCases.addToBudget))
asyncHandler(createExpense(expenseUseCases.addToBudget)),
);

return router;
Expand Down
2 changes: 1 addition & 1 deletion src/usecase/budget/budgetService.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("budgetService", () => {
// THEN
expect(insertBudgetMock).toBeCalledWith(expect.objectContaining(newBudget));
expect(insertedBudgetSummary).toEqual(
expect.objectContaining(expectedBudget)
expect.objectContaining(expectedBudget),
);
});
});
4 changes: 2 additions & 2 deletions src/usecase/budget/classicBudgetService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type BudgetRepository from "../../repository/budget/budgetRepository";
class ClassicBudgetService implements BudgetUseCases {
constructor(
private readonly budgetSummaryRepo: BudgetSummaryRepository,
private readonly budgetRepo: BudgetRepository
private readonly budgetRepo: BudgetRepository,
) {}

async createBudget(newBudget: NewBudget): Promise<Budget> {
Expand All @@ -48,7 +48,7 @@ class ClassicBudgetService implements BudgetUseCases {
}

async getBudgetSummary(
budgetId: BudgetId
budgetId: BudgetId,
): Promise<BudgetSummary | undefined> {
return await this.budgetSummaryRepo.find(budgetId);
}
Expand Down
8 changes: 4 additions & 4 deletions src/usecase/expense/expenseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import currency from "currency.js";

const addNewExpenseToBudget: (
findAll: ExpenseRepository["findAllForBudget"],
insert: ExpenseRepository["insert"]
insert: ExpenseRepository["insert"],
) => ExpenseUseCases["addToBudget"] =
(findAll, insert) => async (budgetId, newExpense) => {
const existingExpenses = await findAll(budgetId);
Expand Down Expand Up @@ -34,15 +34,15 @@ const addNewExpenseToBudget: (
const calculateSpent = (expenses: Expense[], newExpense: Expense): number =>
expenses.reduce(
(prev, curr) => prev.add(curr.amount),
currency(newExpense.amount)
currency(newExpense.amount),
).value;

const ExpenseService: (
expenseRepository: ExpenseRepository
expenseRepository: ExpenseRepository,
) => ExpenseUseCases = (expenseRepository) => ({
addToBudget: addNewExpenseToBudget(
expenseRepository.findAllForBudget,
expenseRepository.insert
expenseRepository.insert,
),
});

Expand Down
2 changes: 1 addition & 1 deletion src/usecase/expense/expenseUseCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface ExpenseUseCases {
*/
addToBudget: (
budgetId: BudgetId,
newExpense: NewExpense
newExpense: NewExpense,
) => Promise<Expense | undefined>;
}

Expand Down

0 comments on commit 71db953

Please sign in to comment.