date-fns#compareAsc TypeScript Examples

The following examples show how to use date-fns#compareAsc. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.ts    From integration-services with Apache License 2.0 6 votes vote down vote up
getLogs = async (req: AuthenticatedRequest, res: Response, next: NextFunction): Promise<Response<any>> => {
		try {
			const channelAddress = lodashGet(req, 'params.channelAddress');
			const { id } = req.user;

			if (!channelAddress || !id) {
				return res.status(StatusCodes.BAD_REQUEST).send({ error: 'no channelAddress or id provided' });
			}
			const startDate = <string>req.query['start-date'];
			const endDate = <string>req.query['end-date'];
			const tempStartDate = getDateFromString(startDate);
			const tempEndDate = getDateFromString(endDate);

			if (startDate && endDate && compareAsc(tempStartDate, tempEndDate) === 1) {
				return res.status(StatusCodes.BAD_REQUEST).send({ error: 'start date is after end date' });
			}

			const limitParam = parseInt(<string>req.query.limit, 10);
			const indexParam = parseInt(<string>req.query.index, 10);
			const limit = isNaN(limitParam) || limitParam == 0 ? undefined : limitParam;
			const index = isNaN(indexParam) ? undefined : indexParam;
			const ascending: boolean = <string>req.query.asc === 'true';
			const options: ChannelLogRequestOptions =
				limit !== undefined && index !== undefined ? { limit, index, ascending, startDate, endDate } : { ascending, startDate, endDate };

			const channel = await this.channelService.getLogs(channelAddress, id, options);
			return res.status(StatusCodes.OK).send(channel);
		} catch (error) {
			this.logger.error(error);
			next(new Error('could not get the logs'));
		}
	};
Example #2
Source File: document.ts    From MDDL with MIT License 6 votes vote down vote up
documentsInAnyCollectionWithGrantAndOwner = async (
  ownerId: string,
  requirementType: string,
  requirementValue: string,
): Promise<SharedDocument[]> => {
  const foundDocuments = (await Document.query()
    .join(
      CollectionDocument.tableName,
      Document.ref('id'),
      CollectionDocument.ref('documentId'),
    )
    .join(
      CollectionGrant.tableName,
      CollectionDocument.ref('collectionId'),
      CollectionGrant.ref('collectionId'),
    )
    .where(CollectionGrant.ref('requirementType'), requirementType)
    .where(CollectionGrant.ref('requirementValue'), requirementValue)
    .where(CollectionGrant.ref('ownerId'), ownerId)
    .modify('fieldsForList')
    .select(
      CollectionGrant.ref('createdBy  as grantCreatedBy'),
      CollectionGrant.ref('createdAt as grantCreatedAt'),
      CollectionDocument.ref('createdBy as documentCollectionCreatedBy'),
      CollectionDocument.ref('createdAt as documentCollectionCreatedAt'),
    )
    .orderBy(Document.ref('createdAt'))) as SharedDocument[]

  // we sort ascending here as we will flip the array after removing duplicates
  const sortedDocuments = foundDocuments.sort((d1, d2) => {
    return compareAsc(
      max([d1.documentCollectionCreatedAt, d1.grantCreatedAt]),
      max([d2.documentCollectionCreatedAt, d2.grantCreatedAt]),
    )
  })
  const deduplicatedDocumentsObj: { [index: string]: SharedDocument } = {}
  sortedDocuments.map((d) => (deduplicatedDocumentsObj[d.id] = d))
  return Object.values(deduplicatedDocumentsObj).reverse()
}