link.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import bcrypt from "bcryptjs";
  2. import { CustomError } from "../utils";
  3. import * as redis from "../redis";
  4. import knex from "../knex";
  5. const selectable = [
  6. "links.id",
  7. "links.address",
  8. "links.banned",
  9. "links.created_at",
  10. "links.domain_id",
  11. "links.updated_at",
  12. "links.password",
  13. "links.description",
  14. "links.target",
  15. "links.visit_count",
  16. "links.user_id",
  17. "links.uuid",
  18. "domains.address as domain"
  19. ];
  20. const normalizeMatch = (match: Partial<Link>): Partial<Link> => {
  21. const newMatch = { ...match };
  22. if (newMatch.address) {
  23. newMatch["links.address"] = newMatch.address;
  24. delete newMatch.address;
  25. }
  26. if (newMatch.user_id) {
  27. newMatch["links.user_id"] = newMatch.user_id;
  28. delete newMatch.user_id;
  29. }
  30. if (newMatch.uuid) {
  31. newMatch["links.uuid"] = newMatch.uuid;
  32. delete newMatch.uuid;
  33. }
  34. return newMatch;
  35. };
  36. interface TotalParams {
  37. search?: string;
  38. }
  39. export const total = async (match: Match<Link>, params: TotalParams = {}) => {
  40. const query = knex<Link>("links");
  41. Object.entries(match).forEach(([key, value]) => {
  42. query.andWhere(key, ...(Array.isArray(value) ? value : [value]));
  43. });
  44. if (params.search) {
  45. query.andWhereRaw(
  46. "links.description || ' ' || links.address || ' ' || target ILIKE '%' || ? || '%'",
  47. [params.search]
  48. );
  49. }
  50. const [{ count }] = await query.count("id");
  51. return typeof count === "number" ? count : parseInt(count);
  52. };
  53. interface GetParams {
  54. limit: number;
  55. search?: string;
  56. skip: number;
  57. }
  58. export const get = async (match: Partial<Link>, params: GetParams) => {
  59. const query = knex<LinkJoinedDomain>("links")
  60. .select(...selectable)
  61. .where(normalizeMatch(match))
  62. .offset(params.skip)
  63. .limit(params.limit)
  64. .orderBy("created_at", "desc");
  65. if (params.search) {
  66. query.andWhereRaw(
  67. "links.description || ' ' || links.address || ' ' || target ILIKE '%' || ? || '%'",
  68. [params.search]
  69. );
  70. }
  71. query.leftJoin("domains", "links.domain_id", "domains.id");
  72. const links: LinkJoinedDomain[] = await query;
  73. return links;
  74. };
  75. export const find = async (match: Partial<Link>): Promise<Link> => {
  76. if (match.address && match.domain_id) {
  77. const key = redis.key.link(match.address, match.domain_id);
  78. const cachedLink = await redis.get(key);
  79. if (cachedLink) return JSON.parse(cachedLink);
  80. }
  81. const link = await knex<Link>("links")
  82. .select(...selectable)
  83. .where(normalizeMatch(match))
  84. .leftJoin("domains", "links.domain_id", "domains.id")
  85. .first();
  86. if (link) {
  87. const key = redis.key.link(link.address, link.domain_id);
  88. redis.set(key, JSON.stringify(link), "EX", 60 * 60 * 2);
  89. }
  90. return link;
  91. };
  92. interface Create extends Partial<Link> {
  93. address: string;
  94. target: string;
  95. }
  96. export const create = async (params: Create) => {
  97. let encryptedPassword: string = null;
  98. if (params.password) {
  99. const salt = await bcrypt.genSalt(12);
  100. encryptedPassword = await bcrypt.hash(params.password, salt);
  101. }
  102. const [link]: LinkJoinedDomain[] = await knex<LinkJoinedDomain>(
  103. "links"
  104. ).insert(
  105. {
  106. password: encryptedPassword,
  107. domain_id: params.domain_id || null,
  108. user_id: params.user_id || null,
  109. address: params.address,
  110. description: params.description || null,
  111. target: params.target
  112. },
  113. "*"
  114. );
  115. return link;
  116. };
  117. export const remove = async (match: Partial<Link>) => {
  118. const link = await knex<Link>("links")
  119. .where(match)
  120. .first();
  121. if (!link) {
  122. throw new CustomError("Link was not found.");
  123. }
  124. const deletedLink = await knex<Link>("links")
  125. .where("id", link.id)
  126. .delete();
  127. redis.remove.link(link);
  128. return !!deletedLink;
  129. };
  130. export const update = async (match: Partial<Link>, update: Partial<Link>) => {
  131. const links = await knex<Link>("links")
  132. .where(match)
  133. .update({ ...update, updated_at: new Date().toISOString() }, "*");
  134. links.forEach(redis.remove.link);
  135. return links;
  136. };
  137. export const increamentVisit = async (match: Partial<Link>) => {
  138. return knex<Link>("links")
  139. .where(match)
  140. .increment("visit_count", 1);
  141. };