Skip to content

Commit ce292c7

Browse files
authored
Hosted Collective: add startsAt order option (#11397)
* feat: add startsAt order option * fix: when sorting by startsAt, filter out null values * fix: also add startsAt filtering
1 parent 42a1a5c commit ce292c7

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

server/graphql/v2/object/Host.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,7 @@ export const GraphQLHost = new GraphQLObjectType({
15951595
SELECT v FROM (
15961596
SELECT v::text::int FROM (SELECT jsonb_array_elements(data#>'{visibleToAccountIds}') as v)
15971597
) WHERE v = ANY(ARRAY[${accountIds.map(v => sequelize.escape(v)).join(',')}]::int[])
1598-
)
1598+
)
15991599
)
16001600
`),
16011601
];
@@ -1741,11 +1741,20 @@ export const GraphQLHost = new GraphQLObjectType({
17411741
type: new GraphQLList(GraphQLString),
17421742
description: 'Filter by specific Account currencies',
17431743
},
1744+
startsAtFrom: {
1745+
type: GraphQLDateTime,
1746+
description: 'Filter for accounts (Events) that started at a specific date range',
1747+
},
1748+
startsAtTo: {
1749+
type: GraphQLDateTime,
1750+
description: 'Filter for accounts (Events) that started at a specific date range',
1751+
},
17441752
},
17451753
async resolve(host, args) {
17461754
const where: Parameters<typeof models.Collective.findAndCountAll>[0]['where'] = {
17471755
HostCollectiveId: host.id,
17481756
id: { [Op.not]: host.id },
1757+
[Op.and]: [],
17491758
};
17501759

17511760
if (args.accountType && args.accountType.length > 0) {
@@ -1787,12 +1796,6 @@ export const GraphQLHost = new GraphQLObjectType({
17871796
assert(args.balance.lte.currency === host.currency, 'Balance currency must match host currency');
17881797
}
17891798

1790-
// @ts-expect-error Type 'unique symbol' cannot be used as an index type. Not sure why TS is not happy here.
1791-
if (!where[Op.and]) {
1792-
// @ts-expect-error Type 'unique symbol' cannot be used as an index type. Not sure why TS is not happy here.
1793-
where[Op.and] = [];
1794-
}
1795-
17961799
const { operator, value } = getAmountRangeValueAndOperator(args.balance);
17971800
// @ts-expect-error Type 'unique symbol' cannot be used as an index type. Not sure why TS is not happy here.
17981801
where[Op.and].push(sequelize.where(ACCOUNT_BALANCE_QUERY, operator, value));
@@ -1813,12 +1816,6 @@ export const GraphQLHost = new GraphQLObjectType({
18131816
);
18141817
}
18151818

1816-
// @ts-expect-error Type 'unique symbol' cannot be used as an index type. Not sure why TS is not happy here.
1817-
if (!where[Op.and]) {
1818-
// @ts-expect-error Type 'unique symbol' cannot be used as an index type. Not sure why TS is not happy here.
1819-
where[Op.and] = [];
1820-
}
1821-
18221819
const { operator, value } = getAmountRangeValueAndOperator(args.consolidatedBalance);
18231820
// @ts-expect-error Type 'unique symbol' cannot be used as an index type. Not sure why TS is not happy here.
18241821
where[Op.and].push(sequelize.where(ACCOUNT_CONSOLIDATED_BALANCE_QUERY, operator, value));
@@ -1857,6 +1854,15 @@ export const GraphQLHost = new GraphQLObjectType({
18571854
where[Op.or] = searchTermConditions;
18581855
}
18591856

1857+
if (args.startsAtFrom) {
1858+
// @ts-expect-error Type 'unique symbol' cannot be used as an index type. Not sure why TS is not happy here.
1859+
where[Op.and].push({ startsAt: { [Op.gte]: args.startsAtFrom } });
1860+
}
1861+
if (args.startsAtTo) {
1862+
// @ts-expect-error Type 'unique symbol' cannot be used as an index type. Not sure why TS is not happy here.
1863+
where[Op.and].push({ startsAt: { [Op.lte]: args.startsAtTo } });
1864+
}
1865+
18601866
const orderBy = [];
18611867
if (args.orderBy) {
18621868
const { field, direction } = args.orderBy;
@@ -1874,6 +1880,9 @@ export const GraphQLHost = new GraphQLObjectType({
18741880
),
18751881
direction,
18761882
]);
1883+
} else if (field === ORDER_BY_PSEUDO_FIELDS.STARTS_AT) {
1884+
where['startsAt'] = { [Op.not]: null };
1885+
orderBy.push(['startsAt', direction]);
18771886
} else {
18781887
orderBy.push([field, direction]);
18791888
}

0 commit comments

Comments
 (0)