(
userId: string,
cost: number,
description: string,
budget: string,
date: string,
is_recurring: boolean,
{ skipRecalculation = false, skipBudgetMatching = false, skipBudgetCreation = false }: {
skipRecalculation?: boolean;
skipBudgetMatching?: boolean;
skipBudgetCreation?: boolean;
} = {},
)
| 242 | } |
| 243 | |
| 244 | static async create( |
| 245 | userId: string, |
| 246 | cost: number, |
| 247 | description: string, |
| 248 | budget: string, |
| 249 | date: string, |
| 250 | is_recurring: boolean, |
| 251 | { skipRecalculation = false, skipBudgetMatching = false, skipBudgetCreation = false }: { |
| 252 | skipRecalculation?: boolean; |
| 253 | skipBudgetMatching?: boolean; |
| 254 | skipBudgetCreation?: boolean; |
| 255 | } = {}, |
| 256 | ) { |
| 257 | const extra: Expense['extra'] = {}; |
| 258 | |
| 259 | if (!budget.trim()) { |
| 260 | budget = 'Misc'; |
| 261 | } |
| 262 | |
| 263 | // Match budget to an existing expense "by default" |
| 264 | if (!skipBudgetMatching && budget === 'Misc') { |
| 265 | const existingExpense = await this.getByName(userId, description); |
| 266 | |
| 267 | if (existingExpense) { |
| 268 | budget = existingExpense.budget; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | if (!skipBudgetCreation) { |
| 273 | const existingBudgetInMonth = await BudgetModel.getByName(userId, date.substring(0, 7), budget); |
| 274 | |
| 275 | if (!existingBudgetInMonth) { |
| 276 | await BudgetModel.create(userId, budget, date.substring(0, 7), 100); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | const newExpense = (await db.query<Expense>( |
| 281 | sql`INSERT INTO "bewcloud_expenses" ( |
| 282 | "user_id", |
| 283 | "cost", |
| 284 | "description", |
| 285 | "budget", |
| 286 | "date", |
| 287 | "is_recurring", |
| 288 | "extra" |
| 289 | ) VALUES ($1, $2, $3, $4, $5, $6, $7) |
| 290 | RETURNING *`, |
| 291 | [ |
| 292 | userId, |
| 293 | cost, |
| 294 | description, |
| 295 | budget, |
| 296 | date, |
| 297 | is_recurring, |
| 298 | JSON.stringify(extra), |
| 299 | ], |
| 300 | ))[0]; |
| 301 |
nothing calls this directly
no test coverage detected