Skip to content

Commit

Permalink
Merge pull request #38 from danutzcodrescu/feat/edit-invoice-offer-ex…
Browse files Browse the repository at this point in the history
…pense

feat: edit invoices, expenses and offers
  • Loading branch information
danutzcodrescu authored Apr 12, 2020
2 parents 4fab261 + 9495376 commit 80edead
Show file tree
Hide file tree
Showing 29 changed files with 894 additions and 67 deletions.
2 changes: 1 addition & 1 deletion src/backend/entities/Base.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
@Entity()
@ObjectType()
export class Base extends BaseEntity {
@Field(type => ID)
@Field(() => ID)
@PrimaryGeneratedColumn('uuid')
readonly id: string;

Expand Down
4 changes: 4 additions & 0 deletions src/backend/entities/Expense.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export class Expense extends BaseEntity {
@BeforeUpdate()
updateDate() {
this.updatedAt = format(new Date(), 'yyyy-mm-dd HH:MM:SS');
this.invoiceDate = format(
new Date(this.invoiceDate),
'yyyy-MM-dd HH:mm:SS',
);
}

@BeforeInsert()
Expand Down
6 changes: 5 additions & 1 deletion src/backend/entities/Invoice.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class Invoice extends BaseEntity {
invoiceDate: string;

@Field()
@Column({ type: 'text', nullable: false })
@Column({ type: 'text', nullable: false, unique: true })
invoiceNumber: string;

@Field()
Expand Down Expand Up @@ -70,6 +70,10 @@ export class Invoice extends BaseEntity {
@BeforeUpdate()
updateDate() {
this.updatedAt = format(new Date(), 'yyyy-mm-dd HH:MM:SS');
this.invoiceDate = format(
new Date(this.invoiceDate),
'yyyy-MM-dd HH:mm:SS',
);
}

@BeforeInsert()
Expand Down
13 changes: 12 additions & 1 deletion src/backend/entities/Offer.entity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { format } from 'date-fns';
import { Field, ObjectType } from 'type-graphql';
import { BeforeInsert, Column, Entity, ManyToOne } from 'typeorm';
import { BeforeInsert, BeforeUpdate, Column, Entity, ManyToOne } from 'typeorm';
import { Base } from './Base.entity';
import { Client } from './Client.entity';
import { Profile } from './Profile.entity';
Expand Down Expand Up @@ -53,8 +53,19 @@ export class Offer extends Base {
@BeforeInsert()
createDate() {
this.createdAt = format(new Date(), 'yyyy-MM:dd HH:mm:SS');
console.log('created', this.createdAt);
this.updatedAt = format(new Date(), 'yyyy-MM-dd HH:mm:SS');
console.log('update', this.updatedAt);
this.invoiceDate = format(
new Date(this.invoiceDate),
'yyyy-MM-dd HH:mm:SS',
);
this.validUntil = format(new Date(this.validUntil), 'yyyy-MM-dd HH:mm:SS');
}

@BeforeUpdate()
updateDate() {
this.updatedAt = format(new Date(), 'yyyy-mm-dd HH:MM:SS');
this.invoiceDate = format(
new Date(this.invoiceDate),
'yyyy-MM-dd HH:mm:SS',
Expand Down
18 changes: 4 additions & 14 deletions src/backend/entities/VatRule.entity.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import { format } from 'date-fns';
import { Field, ID, ObjectType } from 'type-graphql';
import {
BaseEntity,
BeforeInsert,
BeforeUpdate,
Column,
Entity,
PrimaryGeneratedColumn,
} from 'typeorm';
import { Field, ObjectType } from 'type-graphql';
import { BeforeInsert, BeforeUpdate, Column, Entity } from 'typeorm';
import { Base } from './Base.entity';

@Entity()
@ObjectType()
export class VatRule extends BaseEntity {
@Field(type => ID)
@PrimaryGeneratedColumn('uuid')
readonly id: string;

export class VatRule extends Base {
@Field()
@Column()
name: string;
Expand Down
15 changes: 15 additions & 0 deletions src/backend/resolvers/expense.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { EntityManager } from 'typeorm';
import { InjectManager } from 'typeorm-typedi-extensions';
import { Expense } from '../entities/Expense.entity';
import { setParams } from '../utils/helpers';
import { ExpenseUpdate } from './types/arguments.helpers';
import { CreateExpense } from './types/operations.helpers';

@Resolver(Expense)
Expand All @@ -27,6 +28,10 @@ export class ExpenseResolver {
// @ts-ignore
return this.entityManager.find(Expense, params);
}
@Query(() => Expense)
getExpense(@Arg('id', () => ID) id: string) {
return this.entityManager.findOne(Expense, id);
}

@Mutation(() => Expense)
async createExpense(
Expand All @@ -40,6 +45,16 @@ export class ExpenseResolver {
await this.entityManager.delete(Expense, id);
return true;
}
@Mutation(() => Expense)
async updateExpense(@Arg('data') data: ExpenseUpdate) {
await this.entityManager
.createQueryBuilder()
.update(Expense)
.set(data)
.where({ id: data.id })
.execute();
return this.entityManager.findOne(data.id);
}

@FieldResolver(() => String)
invoiceDate(@Root() expense: Expense) {
Expand Down
35 changes: 34 additions & 1 deletion src/backend/resolvers/invoices.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Invoice, Item } from '../entities/Invoice.entity';
import { Profile, ProfileData } from '../entities/Profile.entity';
import { setParams } from '../utils/helpers';
import { getInvoiceNumber } from '../utils/invoices';
import { InvoiceUpdate } from './types/arguments.helpers';
import {
ClientInput,
InvoiceInput,
Expand Down Expand Up @@ -52,6 +53,11 @@ export class InvoiceResolver {
);
}

@Query(() => Invoice)
async getInvoice(@Arg('id', () => ID) id: string) {
return this.entityManager.findOne(Invoice, id);
}

@Mutation(() => Invoice)
async createInvoice(
@Arg('client') client: ClientInput,
Expand All @@ -65,7 +71,7 @@ export class InvoiceResolver {
const clientDB = await this.entityManager.findOne(Client, client.clientId);
// @ts-ignore
const invoice = this.entityManager.create(Invoice, {
createDate: invoiceData.invoiceDate,
invoiceDate: invoiceData.invoiceDate,
amount: invoiceData.amount,
vat: invoiceData.vat,
items: invoiceData.items,
Expand All @@ -80,6 +86,33 @@ export class InvoiceResolver {
return inv;
}

@Mutation(() => Invoice)
async updateInvoice(@Arg('data') data: InvoiceUpdate) {
const [currentProfile, clientDB] = await Promise.all([
this.entityManager.findOne(Profile, data.profileId),
this.entityManager.findOne(Client, data.clientId),
]);
// @ts-ignore
await this.entityManager
.createQueryBuilder()
.update(Invoice)
.set({
invoiceDate: data.invoiceDate,
amount: data.amount,
vat: data.vat,
items: data.items,
profileId: currentProfile?.id,
profileData: data.profileData,
clientId: clientDB?.id,
clientData: data.clientData,
vatRuleName: data.vatRuleName,
invoiceNumber: data.invoiceNumber,
})
.where({ id: data.id })
.execute();
return this.entityManager.findOne(Invoice, data.id);
}

@Mutation(() => Boolean)
async deleteInvoice(@Arg('id', (type) => ID) id: string) {
await this.entityManager.delete(Invoice, id);
Expand Down
36 changes: 35 additions & 1 deletion src/backend/resolvers/offers.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Offer } from '../entities/Offer.entity';
import { Profile, ProfileData } from '../entities/Profile.entity';
import { setParams } from '../utils/helpers';
import { getInvoiceNumber } from '../utils/invoices';
import { OfferInsert } from './types/arguments.helpers';
import { OfferInsert, OfferUpdate } from './types/arguments.helpers';

@Resolver(Offer)
export class OfferResolver {
Expand All @@ -39,6 +39,11 @@ export class OfferResolver {
return this.entityManager.find(Offer, params);
}

@Query(() => Offer)
async getOffer(@Arg('id', () => ID) id: string) {
return this.entityManager.findOne(Offer, id);
}

@Mutation(() => Offer)
async insertOffer(@Arg('objet') obj: OfferInsert): Promise<Offer> {
const currentProfile = await this.entityManager.findOne(
Expand Down Expand Up @@ -104,6 +109,35 @@ export class OfferResolver {
return true;
}

@Mutation(() => Offer)
async updateOffer(@Arg('data') data: OfferUpdate) {
const [currentProfile, clientDB] = await Promise.all([
this.entityManager.findOne(Profile, data.profileId),
data.clientId
? this.entityManager.findOne(Client, data.clientId)
: undefined,
]);
// @ts-ignore
await this.entityManager
.createQueryBuilder()
.update(Offer)
.set({
invoiceDate: data.invoiceDate,
amount: data.amount,
vat: data.vat,
items: data.items,
profileId: currentProfile?.id,
profileData: data.profileData,
clientId: clientDB?.id,
clientData: data.clientData,
vatRuleName: data.vatRuleName,
validUntil: data.validUntil,
})
.where({ id: data.id })
.execute();
return this.entityManager.findOne(Offer, data.id);
}

@FieldResolver(() => ClientData)
clientData(@Root() offer: Offer) {
return JSON.parse(offer.clientData);
Expand Down
74 changes: 74 additions & 0 deletions src/backend/resolvers/types/arguments.helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Field, InputType } from 'type-graphql';
import { Expense } from '../../entities/Expense.entity';
import { Invoice } from '../../entities/Invoice.entity';
import { Offer } from '../../entities/Offer.entity';

@InputType()
Expand All @@ -24,3 +26,75 @@ export class OfferInsert implements Partial<Offer> {
@Field()
validUntil: string;
}

@InputType()
export class OfferUpdate implements Partial<Offer> {
@Field()
id: string;
@Field()
invoiceDate: string;
@Field({ nullable: true })
clientId?: string;
@Field()
clientData: string;
@Field()
profileData: string;
@Field()
profileId: string;
@Field()
items: string;
@Field()
amount: number;
@Field()
vat: number;
@Field()
vatRuleName: string;
@Field()
validUntil: string;
}

@InputType()
export class InvoiceUpdate implements Partial<Invoice> {
@Field()
id: string;
@Field()
invoiceDate: string;
@Field({ nullable: true })
clientId: string;
@Field()
clientData: string;
@Field()
profileData: string;
@Field({ nullable: true })
profileId: string;
@Field()
items: string;
@Field()
amount: number;
@Field()
vat: number;
@Field()
vatRuleName: string;
@Field()
invoiceNumber: string;
}

@InputType()
export class ExpenseUpdate implements Partial<Expense> {
@Field()
id: string;
@Field()
invoiceDate: string;
@Field({ nullable: true })
clientId: string;
@Field()
clientName: string;
@Field()
amount: number;
@Field()
vat: number;
@Field({ nullable: true })
description: string;
@Field()
invoiceNumber: string;
}
4 changes: 2 additions & 2 deletions src/renderer/components/Application.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { NoSsr } from '@material-ui/core';
import CssBaseline from '@material-ui/core/CssBaseline';
import { ThemeProvider as MaterialUIThemeProvider } from '@material-ui/core/styles';
import { MuiPickersUtilsProvider } from '@material-ui/pickers';
import en from 'date-fns/locale/en-GB';
import * as React from 'react';
import { hot } from 'react-hot-loader/root';
import { HashRouter } from 'react-router-dom';
Expand All @@ -15,7 +16,6 @@ import { theme } from '../theme/theme';
import { Notification } from './notification/Notification.component';
import { Routes } from './Routes';
import { Menu } from './toolbox/Menu.component';

export const Application = () => {
if (window.location.hash.endsWith('invoice')) {
return (
Expand All @@ -40,7 +40,7 @@ export const Application = () => {
<ApolloProvider client={client}>
<ThemeProvider theme={theme}>
<MaterialUIThemeProvider theme={theme}>
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<MuiPickersUtilsProvider utils={DateFnsUtils} locale={en}>
<NotificationProvider>
<Notification />
<HashRouter>
Expand Down
Loading

0 comments on commit 80edead

Please sign in to comment.