Skip to content

Commit

Permalink
enhancement(ExpenseItem): Usa latest sequelize typing strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
Betree committed Aug 18, 2022
1 parent ef080a4 commit 15719b1
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 42 deletions.
2 changes: 1 addition & 1 deletion server/lib/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Model } from 'sequelize';
* @param {Array} diffedFields: The fields used to compare objects for ``
* @returns [newEntries, removedEntries, updatedEntries]
*/
export function diffDBEntries<T extends Model<T>>(
export function diffDBEntries<T extends Model>(
oldEntries: T[],
newEntriesData: Record<string, unknown>[],
diffedFields: string[],
Expand Down
23 changes: 0 additions & 23 deletions server/lib/restore-sequelize-attributes-on-class.ts

This file was deleted.

29 changes: 12 additions & 17 deletions server/models/ExpenseItem.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { pick } from 'lodash';
import type { CreationOptional, InferAttributes, InferCreationAttributes } from 'sequelize';
import { DataTypes, Model, Transaction } from 'sequelize';

import { diffDBEntries } from '../lib/data';
import { isValidUploadedImage } from '../lib/images';
import restoreSequelizeAttributesOnClass from '../lib/restore-sequelize-attributes-on-class';
import { buildSanitizerOptions, sanitizeHTML } from '../lib/sanitize-html';
import sequelize from '../lib/sequelize';

Expand All @@ -12,25 +12,20 @@ import models from '.';
/**
* Sequelize model to represent an ExpenseItem, linked to the `ExpenseItems` table.
*/
export class ExpenseItem extends Model {
public readonly id!: number;
public ExpenseId!: number;
public CreatedByUserId!: number;
public amount!: number;
public url!: string;
public createdAt!: Date;
public updatedAt!: Date;
public deletedAt: Date;
public incurredAt!: Date;
public description: string;
export class ExpenseItem extends Model<InferAttributes<ExpenseItem>, InferCreationAttributes<ExpenseItem>> {
public declare readonly id: CreationOptional<number>;
public declare ExpenseId: number;
public declare CreatedByUserId: number;
public declare amount: number;
public declare url: string;
public declare createdAt: CreationOptional<Date>;
public declare updatedAt: CreationOptional<Date>;
public declare deletedAt: CreationOptional<Date>;
public declare incurredAt: Date;
public declare description: CreationOptional<string>;

private static editableFields = ['amount', 'url', 'description', 'incurredAt'];

constructor(...args) {
super(...args);
restoreSequelizeAttributesOnClass(new.target, this);
}

/**
* Based on `diffDBEntries`, diff two items list to know which ones where
* added, removed or added.
Expand Down
2 changes: 1 addition & 1 deletion test/test-helpers/fake-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export const fakeUpdate = async (updateData: Record<string, unknown> = {}, seque
export const fakeExpenseItem = async (attachmentData: Record<string, unknown> = {}) => {
return models.ExpenseItem.create({
amount: randAmount(),
url: attachmentData.url || `${randUrl()}.pdf`,
url: <string>attachmentData.url || `${randUrl()}.pdf`,
description: randStr(),
...attachmentData,
ExpenseId: attachmentData.ExpenseId || (await fakeExpense({ items: [] })).id,
Expand Down

0 comments on commit 15719b1

Please sign in to comment.