| 105 | * @returns Shortened URL information and statistics, or `null` if it couldn't be found |
| 106 | */ |
| 107 | export async function stats(id: Short): Promise<null | Stats> { |
| 108 | const encodedId = encode(id); |
| 109 | |
| 110 | const [visits, shortenedUrl] = await db.$transaction([ |
| 111 | db.visit.findMany({where: {shortenedUrlId: encodedId}, select: {timestamp: true}, orderBy: {timestamp: 'asc'}}), |
| 112 | db.shortenedUrl.findUnique({where: {shortBase64: encodedId}, select: {url: true}}), |
| 113 | ]); |
| 114 | |
| 115 | if (!shortenedUrl) { |
| 116 | return null; |
| 117 | } |
| 118 | |
| 119 | return {visits: visits.map(visit => visit.timestamp), url: shortenedUrl.url}; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Shorten a long URL |