Skip to content

Commit

Permalink
feat: add documnt search
Browse files Browse the repository at this point in the history
  • Loading branch information
fr-ser committed Nov 24, 2024
1 parent 5e8dddf commit 1f9d0ad
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 43 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,3 @@ These environment variables should be set:

- PI_SSH_PORT
- PI_SSH_ADDRESS

<!-- TODO: remove all kinds of overview search stuff -->
<!-- TODO: add document search endpoint -->
5 changes: 0 additions & 5 deletions backend/scripts/seed-local-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,6 @@ async function insertData(dataSource: DataSource) {
description: `description ${index + 1}`,
});

// const invoiceDocument = await dataSource.manager.findOne(InvoiceDocument, {
// where: { invoice: { order_id: `A${index + 1}` } },
// });
// console.log("invoiceDocument", invoiceDocument);

await dataSource
.createQueryBuilder()
.relation(OverdueNotice, "invoice_documents")
Expand Down
18 changes: 8 additions & 10 deletions backend/src/routes/clients.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import express from "express";
import { SelectQueryBuilder } from "typeorm";

import { getAppDataSource } from "@/db";
import { Client } from "@/db/entities/client";
Expand All @@ -23,17 +24,12 @@ clientsRouter.get(

const dataSource = getAppDataSource();

let result: [Client[], number];
let baseQuery: SelectQueryBuilder<Client>;

// the "simple" case without search
if (!search) {
result = await dataSource.manager.findAndCount(Client, {
skip,
take,
order: { created_at: "DESC" },
});
baseQuery = dataSource.manager.createQueryBuilder(Client, "client");
} else {
const baseQuery = dataSource.manager
baseQuery = dataSource.manager
.createQueryBuilder(Client, "client")
.where(
"first_name || ' ' || last_name LIKE '%' || :nameSearch || '%' " +
Expand All @@ -43,9 +39,11 @@ clientsRouter.get(
companySearch: search,
},
);

result = await Promise.all([baseQuery.take(take).skip(skip).getMany(), baseQuery.getCount()]);
}
const result = await Promise.all([
baseQuery.take(take).skip(skip).orderBy("created_at", "DESC").getMany(),
baseQuery.getCount(),
]);

res.json({ data: result[0], totalCount: result[1] } as PaginationResponse<Client>);
},
Expand Down
69 changes: 48 additions & 21 deletions backend/src/routes/documents/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from "express";
import { In, LessThanOrEqual } from "typeorm";
import { In, LessThanOrEqual, SelectQueryBuilder } from "typeorm";

import { getAppDataSource } from "@/db";
import { InvoiceDocument, OfferDocument, OverdueNoticeDocument } from "@/db/entities/documents";
Expand Down Expand Up @@ -30,30 +30,57 @@ documentsRouter.get(
interface QueryParams {
start_timestamp?: number;
take?: number;
search?: string;
}
const { take = 300 } = req.query as QueryParams;
let { start_timestamp } = req.query as QueryParams;
if (start_timestamp == null) {
start_timestamp = Date.now() / 1000;
const { take = 300, start_timestamp = Date.now() / 1000, search } = req.query as QueryParams;

let baseInvoiceQuery: SelectQueryBuilder<InvoiceDocument>;
let baseOfferQuery: SelectQueryBuilder<OfferDocument>;
let baseOverdueNoticeQuery: SelectQueryBuilder<OverdueNoticeDocument>;

if (!search) {
baseInvoiceQuery = getAppDataSource().manager.createQueryBuilder(InvoiceDocument, "invoice");
baseOfferQuery = getAppDataSource().manager.createQueryBuilder(OfferDocument, "offer");
baseOverdueNoticeQuery = getAppDataSource().manager.createQueryBuilder(
OverdueNoticeDocument,
"overdue_notice",
);
} else {
const cleanSearch = search.trim();
baseInvoiceQuery = getAppDataSource()
.manager.createQueryBuilder(InvoiceDocument, "invoice")
.where("(id LIKE '%' || :search || '%' OR order_title LIKE '%' || :search || '%')", {
search: cleanSearch,
});

baseOfferQuery = getAppDataSource()
.manager.createQueryBuilder(OfferDocument, "offer")
.where("(id LIKE '%' || :search || '%' OR order_title LIKE '%' || :search || '%')", {
search: cleanSearch,
});
baseOverdueNoticeQuery = getAppDataSource()
.manager.createQueryBuilder(OverdueNoticeDocument, "overdue_notice")
.where("(id LIKE '%' || :search || '%' OR order_title LIKE '%' || :search || '%')", {
search: cleanSearch,
});
}

const dataSource = getAppDataSource();
const allDataResult = await Promise.all([
dataSource.manager.find(InvoiceDocument, {
where: { created_at: LessThanOrEqual(start_timestamp) },
order: { created_at: "DESC" },
take: take,
}),
dataSource.manager.find(OfferDocument, {
where: { created_at: LessThanOrEqual(start_timestamp) },
order: { created_at: "DESC" },
take: take,
}),
dataSource.manager.find(OverdueNoticeDocument, {
where: { created_at: LessThanOrEqual(start_timestamp) },
order: { created_at: "DESC" },
take: take,
}),
baseInvoiceQuery
.andWhere({ created_at: LessThanOrEqual(start_timestamp) })
.take(take)
.orderBy("created_at", "DESC")
.getMany(),
baseOfferQuery
.andWhere({ created_at: LessThanOrEqual(start_timestamp) })
.take(take)
.orderBy("created_at", "DESC")
.getMany(),
baseOverdueNoticeQuery
.andWhere({ created_at: LessThanOrEqual(start_timestamp) })
.take(take)
.orderBy("created_at", "DESC")
.getMany(),
]);

res.json(
Expand Down
2 changes: 0 additions & 2 deletions backend/src/routes/orders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ ordersRouter.get(
);
}

console.log(databaseQuery.getSql());

const result = await databaseQuery.getManyAndCount();

res.json({ data: result[0], totalCount: result[1] } as PaginationResponse<Order>);
Expand Down
1 change: 0 additions & 1 deletion backend/src/tests/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { DataSource } from "typeorm";

export async function clearDatabase(dataSource: DataSource) {
const entities = dataSource.entityMetadatas;
console.log(entities);
for (const entity of entities) {
const repository = dataSource.getRepository(entity.name);
await repository.clear();
Expand Down
1 change: 0 additions & 1 deletion backend/src/tests/routes/invoice_documents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ describe("invoiceDocuments routes", () => {
let server: Server;
let appDataSource: DataSource;
const temporaryDirectory = mkdtempSync(path.join(os.tmpdir(), "test-"));
console.log("temporaryDirectory", temporaryDirectory);

beforeAll(async () => {
appDataSource = await initializeAppDataSource(path.join(temporaryDirectory, "test.db"));
Expand Down

0 comments on commit 1f9d0ad

Please sign in to comment.