Skip to content

Commit

Permalink
feat(endpoint): add view with deleted workflow (#6)
Browse files Browse the repository at this point in the history
OlegDO authored Oct 18, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 922c262 commit 4d3b4ad
Showing 2 changed files with 39 additions and 3 deletions.
25 changes: 25 additions & 0 deletions __tests__/services/endpoint-test.ts
Original file line number Diff line number Diff line change
@@ -566,6 +566,17 @@ describe('services/endpoint', () => {
expect(result).to.deep.equal({ entity });
});

it('should run default view handler with query builder and handle has removed: typeorm case', async () => {
const viewWithDefaultHandler = Endpoint.view?.(() => ({ repository }));

const result = await viewWithDefaultHandler({ query: {}, hasRemoved: true }, endpointOptions);
const [queryBuilder, params] = defaultHandlerStub.firstCall.args;

expect(queryBuilder).to.be.instanceof(SelectQueryBuilder);
expect(params?.hasRemoved).to.be.ok;
expect(result).to.deep.equal({ entity });
});

it('should run default view handler with query builder: query builder case', async () => {
handler.resetHistory();
// return query builder from handler
@@ -604,6 +615,20 @@ describe('services/endpoint', () => {
expect(await waitResult(result)).to.throw(emptyConditionMessage);
});

it('handler - should return view entity with removed: default handler', async () => {
defaultHandlerStub.restore();
TypeormMock.queryBuilder.getMany.resolves([entity]);

const qb = repository.createQueryBuilder().where('id = 1');
const withDeletedSpy = sandbox.spy(qb, 'withDeleted');

const result = await Endpoint.defaultHandler.view(qb, { hasRemoved: true });

expect(withDeletedSpy).to.be.calledOnce;
expect(TypeormMock.queryBuilder.getMany).to.be.calledOnce;
expect(result).to.deep.equal({ entity });
});

it('handler - should return cached result', async () => {
defaultHandlerStub.restore();
TypeormMock.queryBuilder.getMany.resolves([entity]);
17 changes: 14 additions & 3 deletions src/services/endpoint.ts
Original file line number Diff line number Diff line change
@@ -127,6 +127,7 @@ class ListRequestParams<TEntity> {
query?: IJsonQuery<TEntity>;

@IsBoolean()
@IsUndefinable()
hasRemoved?: boolean;
}

@@ -147,6 +148,10 @@ class ViewRequestParams<TEntity> {
@IsObject()
@Type(() => IJsonQueryFilter)
query: IJsonQuery<TEntity>;

@IsBoolean()
@IsUndefinable()
hasRemoved?: boolean;
}

class ViewOutputParams<TEntity> {
@@ -732,7 +737,7 @@ const createDefaultHandler = async <TEntity, TResult = never>({
*/
const viewDefaultHandler = async <TEntity>(
query: SelectQueryBuilder<TEntity>,
{ cache = 0 } = {},
{ cache = 0, hasRemoved = false } = {},
): Promise<ViewOutputParams<TEntity>> => {
if (hasEmptyCondition(query)) {
throw new BaseException({
@@ -742,6 +747,10 @@ const viewDefaultHandler = async <TEntity>(
});
}

if (hasRemoved) {
query.withDeleted();
}

if (cache) {
query.cache(getCrudCacheKey(query, CACHE_KEYS.view), cache);
}
@@ -1243,13 +1252,15 @@ class Endpoint {
...queryOptions,
});
const result = await handler(typeQuery, params, options);
const { hasRemoved } = params;
const defaultParams = { hasRemoved, cache };

if (result instanceof TypeormJsonQuery) {
return Endpoint.defaultHandler.view(result.toQuery(), { cache });
return Endpoint.defaultHandler.view(result.toQuery(), defaultParams);
}

if (result instanceof SelectQueryBuilder) {
return Endpoint.defaultHandler.view(result, { cache });
return Endpoint.defaultHandler.view(result, defaultParams);
}

return result;

0 comments on commit 4d3b4ad

Please sign in to comment.