Skip to content

Commit 4cad61e

Browse files
authored
fix(orderscollectionquery): Corrects syntax for querying for searchTerm (#11575)
1 parent 49467f1 commit 4cad61e

File tree

4 files changed

+130
-3
lines changed

4 files changed

+130
-3
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict';
2+
3+
/** @type {import('sequelize-cli').Migration} */
4+
module.exports = {
5+
async up(queryInterface) {
6+
await queryInterface.sequelize.query(`
7+
drop index concurrently if exists "Orders_data_fromAccountInfo_name_search_idx";
8+
`);
9+
await queryInterface.sequelize.query(`
10+
drop index concurrently if exists "Orders_data_fromAccountInfo_email_search_idx";
11+
`);
12+
13+
await queryInterface.sequelize.query(`
14+
create index concurrently if not exists "Orders_data_fromAccountInfo_name_search_idx"
15+
on "Orders" using gist(("data"#>>'{fromAccountInfo,name}') gist_trgm_ops)
16+
include (
17+
description,
18+
tags,
19+
id,
20+
"SubscriptionId",
21+
"createdAt",
22+
"CollectiveId",
23+
"FromCollectiveId",
24+
"CreatedByUserId",
25+
"status",
26+
"TierId",
27+
"interval",
28+
"totalAmount",
29+
"currency",
30+
"PaymentMethodId",
31+
"deletedAt"
32+
);
33+
`);
34+
35+
await queryInterface.sequelize.query(`
36+
create index concurrently if not exists "Orders_data_fromAccountInfo_email_search_idx"
37+
on "Orders" using gist(("data"#>>'{fromAccountInfo,email}') gist_trgm_ops)
38+
include (
39+
description,
40+
tags,
41+
id,
42+
"SubscriptionId",
43+
"createdAt",
44+
"CollectiveId",
45+
"FromCollectiveId",
46+
"CreatedByUserId",
47+
"status",
48+
"TierId",
49+
"interval",
50+
"totalAmount",
51+
"currency",
52+
"PaymentMethodId",
53+
"deletedAt"
54+
);
55+
`);
56+
},
57+
58+
async down() {
59+
console.log(
60+
'Not rolling back, re-create the "Orders_data_fromAccountInfo_name_search_idx" and "Orders_data_fromAccountInfo_email_search_idx" indexes manually if needed',
61+
);
62+
},
63+
};

server/graphql/v2/query/collection/OrdersCollectionQuery.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,8 @@ export const OrdersCollectionResolver = async (args: OrdersCollectionArgsType, r
517517

518518
ors.push(eb('Orders.description', 'ilike', `%${args.searchTerm}%`));
519519
ors.push(eb(sql`"Orders".data->>'ponumber'`, 'ilike', `%${args.searchTerm}%`));
520-
ors.push(eb(sql`"Orders".data->>'{fromAccountInfo,name}'`, 'ilike', `%${args.searchTerm}%`));
521-
ors.push(eb(sql`"Orders".data->>'{fromAccountInfo,email}'`, 'ilike', `%${args.searchTerm}%`));
520+
ors.push(eb(sql`"Orders".data#>>'{fromAccountInfo,name}'`, 'ilike', `%${args.searchTerm}%`));
521+
ors.push(eb(sql`"Orders".data#>>'{fromAccountInfo,email}'`, 'ilike', `%${args.searchTerm}%`));
522522
ors.push(
523523
eb('Orders.tags', '&&', sql<string[]>`ARRAY[${args.searchTerm.toLocaleLowerCase()}]::varchar[]`),
524524
);

test/server/graphql/v2/collection/OrdersCollectionQuery.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const ordersQuery = gql`
2424
$includeHostedAccounts: Boolean
2525
$accountingCategory: [String]
2626
$createdBy: [AccountReferenceInput]
27+
$searchTerm: String
2728
) {
2829
orders(
2930
account: $account
@@ -34,6 +35,7 @@ const ordersQuery = gql`
3435
includeHostedAccounts: $includeHostedAccounts
3536
accountingCategory: $accountingCategory
3637
createdBy: $createdBy
38+
searchTerm: $searchTerm
3739
) {
3840
totalCount
3941
nodes {
@@ -928,6 +930,59 @@ describe('server/graphql/v2/collection/OrdersCollectionQuery', () => {
928930
});
929931
});
930932

933+
describe('searchTerm argument', () => {
934+
let collective, user1, user2, order1, order2;
935+
936+
before(async () => {
937+
collective = await fakeCollective();
938+
939+
// Create users with specific names for search testing
940+
user1 = await fakeUser(null, { name: 'Alice Anderson' });
941+
user2 = await fakeUser(null, { name: 'Bob Builder' });
942+
943+
// Create orders by different users
944+
order1 = await fakeOrder({
945+
CollectiveId: collective.id,
946+
FromCollectiveId: user1.CollectiveId,
947+
CreatedByUserId: user1.id,
948+
status: OrderStatuses.PAID,
949+
});
950+
951+
order2 = await fakeOrder({
952+
CollectiveId: collective.id,
953+
FromCollectiveId: user2.CollectiveId,
954+
CreatedByUserId: user2.id,
955+
status: OrderStatuses.PAID,
956+
});
957+
});
958+
959+
it("should return the order when the search term matches a user's name", async () => {
960+
const result = await graphqlQueryV2(ordersQuery, {
961+
account: { legacyId: collective.id },
962+
filter: 'INCOMING',
963+
searchTerm: 'Alice',
964+
});
965+
966+
expect(result.errors).to.not.exist;
967+
expect(result.data.orders.totalCount).to.eq(1);
968+
const orderIds = result.data.orders.nodes.map(node => node.legacyId);
969+
expect(orderIds).to.include(order1.id);
970+
});
971+
972+
it("should return the order when the search term matches a user's email", async () => {
973+
const result = await graphqlQueryV2(ordersQuery, {
974+
account: { legacyId: collective.id },
975+
filter: 'INCOMING',
976+
searchTerm: user2.email,
977+
});
978+
979+
expect(result.errors).to.not.exist;
980+
expect(result.data.orders.totalCount).to.eq(1);
981+
const orderIds = result.data.orders.nodes.map(node => node.legacyId);
982+
expect(orderIds).to.include(order2.id);
983+
});
984+
});
985+
931986
describe('createdByUsers resolver', () => {
932987
let collective, childAccount, user1, user2, user3;
933988

test/test-helpers/fake-data.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,8 @@ export const fakeOrder = async (
736736
) => {
737737
const CreatedByUserId = orderData.CreatedByUserId || (await fakeUser()).id;
738738
const user = await models.User.findByPk(<number>CreatedByUserId);
739-
const FromCollectiveId = orderData.FromCollectiveId || (await models.Collective.findByPk(user.CollectiveId)).id;
739+
const fromCollective = await models.Collective.findByPk(user.CollectiveId);
740+
const FromCollectiveId = orderData.FromCollectiveId || fromCollective.id;
740741
const collective = orderData.CollectiveId
741742
? await models.Collective.findByPk(orderData.CollectiveId)
742743
: await fakeCollective();
@@ -745,6 +746,13 @@ export const fakeOrder = async (
745746
: withTier
746747
? await fakeTier()
747748
: null;
749+
const data = {
750+
...orderData.data,
751+
fromAccountInfo: {
752+
name: fromCollective.name,
753+
email: user.email,
754+
},
755+
};
748756

749757
const order: Order & {
750758
subscription?: typeof Subscription;
@@ -759,6 +767,7 @@ export const fakeOrder = async (
759767
CreatedByUserId,
760768
FromCollectiveId,
761769
CollectiveId: collective.id,
770+
data,
762771
});
763772

764773
if (order.PaymentMethodId) {

0 commit comments

Comments
 (0)