From c1a019a4614bdbbc3976eb5bc813c13444643085 Mon Sep 17 00:00:00 2001 From: Thanu Poptiphueng Date: Thu, 16 May 2024 17:26:27 +0700 Subject: [PATCH] lint --- .eslintrc.json | 8 +- app/grouping/GroupCreator.tsx | 6 +- app/grouping/Grouping.tsx | 6 +- app/grouping/page.tsx | 4 +- app/inside/IdComponent.tsx | 12 +- app/inside/page.tsx | 2 +- components/LocationSelector/index.tsx | 12 +- components/locationContext.tsx | 4 +- package.json | 4 +- pnpm-lock.yaml | 196 +++++++++++++++++++++++++- src/minio.ts | 6 +- src/trpc.ts | 6 +- src/userRoute.ts | 43 +++--- 13 files changed, 246 insertions(+), 63 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 51f1e6f..cd91535 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,8 +1,10 @@ { "extends": [ "next/core-web-vitals", - "next", - "prettier", + "plugin:@typescript-eslint/recommended", "plugin:prettier/recommended" - ] + ], + "rules": { + "prefer-const": "error" + } } diff --git a/app/grouping/GroupCreator.tsx b/app/grouping/GroupCreator.tsx index f18f757..86aca62 100644 --- a/app/grouping/GroupCreator.tsx +++ b/app/grouping/GroupCreator.tsx @@ -18,9 +18,9 @@ type Group = { jobs: number[]; }; export default function GroupCreator({ allJobs }: Props) { - let locationContext = useContext(LocationContext); - let [usedJobs, setUsedJobs] = useState([]); - let [groups, setGroup] = useState( + const locationContext = useContext(LocationContext); + const [usedJobs, setUsedJobs] = useState([]); + const [groups, setGroup] = useState( [...Array(4).keys()].map((i) => ({ id: i + 1, jobs: [] })), ); function useJob(id: number) { diff --git a/app/grouping/Grouping.tsx b/app/grouping/Grouping.tsx index b47e1f6..791001a 100644 --- a/app/grouping/Grouping.tsx +++ b/app/grouping/Grouping.tsx @@ -14,10 +14,10 @@ export default function Grouping({ removeJob, updateGroup, }: Props) { - let [selectedJob, setSelectedJob] = useState([]); + const [selectedJob, setSelectedJob] = useState([]); function addJob(id: string) { - let _id = parseInt(id); - let job = availableJobs.find((j) => j.id == _id); + const _id = parseInt(id); + const job = availableJobs.find((j) => j.id == _id); if (job == undefined) return; setSelectedJob((old) => [...old, job]); selectJob(_id); diff --git a/app/grouping/page.tsx b/app/grouping/page.tsx index aaf5b9e..c35ffab 100644 --- a/app/grouping/page.tsx +++ b/app/grouping/page.tsx @@ -4,10 +4,10 @@ import LocationContextProvider from "@/components/locationContext"; import GroupCreator from "./GroupCreator"; export default async function Page() { - let provinces = await db.query.province + const provinces = await db.query.province .findMany({ with: { zones: true } }) .execute(); - let jobList = await db.query.group.findMany().execute(); + const jobList = await db.query.group.findMany().execute(); return ( diff --git a/app/inside/IdComponent.tsx b/app/inside/IdComponent.tsx index f5e33f2..437ab72 100644 --- a/app/inside/IdComponent.tsx +++ b/app/inside/IdComponent.tsx @@ -5,11 +5,11 @@ type Props = { }; export default function IdComponent({ updateIdList }: Props) { - let [idSet, setIdSet] = useState>(new Set()); - let onValidId = (id: string) => { + const [idSet, setIdSet] = useState>(new Set()); + const onValidId = (id: string) => { setIdSet((prev) => new Set(prev.add(id))); }; - let removeCid = (id: string) => { + const removeCid = (id: string) => { setIdSet((prev) => new Set([...prev].filter((x) => x !== id))); }; useEffect(() => { @@ -51,10 +51,10 @@ type SingleIdProps = { }; function SingleIdComponent({ onValidId }: SingleIdProps) { - let [isValid, setIsValid] = useState(false); - let [cid, setCid] = useState(""); + const [isValid, setIsValid] = useState(false); + const [cid, setCid] = useState(""); useEffect(() => { - let isValidId = isValidThaiID(cid); + const isValidId = isValidThaiID(cid); setIsValid(isValidId); if (isValidId) { onValidId(cid); diff --git a/app/inside/page.tsx b/app/inside/page.tsx index 450f5b6..c94b4de 100644 --- a/app/inside/page.tsx +++ b/app/inside/page.tsx @@ -3,7 +3,7 @@ import { useState } from "react"; import IdComponent from "./IdComponent"; export default function Page() { - let [idList, setIdList] = useState([]); + const [idList, setIdList] = useState([]); function submit() {} //TODO! submit inside user return (
diff --git a/components/LocationSelector/index.tsx b/components/LocationSelector/index.tsx index 2abcecc..1a02f0f 100644 --- a/components/LocationSelector/index.tsx +++ b/components/LocationSelector/index.tsx @@ -17,20 +17,20 @@ type Zone = { province: number; }; export default function LocationSelector({ provinces }: Props) { - let [provinceId, setProvinceId] = useState(undefined); - let [amphurList, setAmphurList] = useState(undefined); - let [amphurId, setAmphurId] = useState(undefined); + const [provinceId, setProvinceId] = useState(undefined); + const [amphurList, setAmphurList] = useState(undefined); + const [amphurId, setAmphurId] = useState(undefined); const locationContext = useContext(LocationContext); function setProvince(_id: string) { - let id = parseInt(_id); + const id = parseInt(_id); setProvinceId(id); - let province = provinces.find((p) => p.id == id); + const province = provinces.find((p) => p.id == id); if (province == undefined) return; setAmphurList(province.zones); setAmphurId(undefined); } function setAmphur(_id: string) { - let id = parseInt(_id); + const id = parseInt(_id); setAmphurId(id); } useEffect(() => { diff --git a/components/locationContext.tsx b/components/locationContext.tsx index 342c978..627ff21 100644 --- a/components/locationContext.tsx +++ b/components/locationContext.tsx @@ -21,8 +21,8 @@ export default function LocationContextProvider({ }: { children?: ReactNode; }) { - let zone = useState(undefined); - let province = useState(undefined); + const zone = useState(undefined); + const province = useState(undefined); return ( diff --git a/package.json b/package.json index 062b839..24e8bc2 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "zod": "^3.22.4" }, "devDependencies": { + "@eslint/js": "^9.2.0", "@swc-node/register": "^1.9.0", "@swc/cli": "^0.3.12", "@swc/core": "^1.4.16", @@ -52,6 +53,7 @@ "prettier-plugin-tailwindcss": "^0.5.14", "tailwindcss": "^3.4.3", "ts-node": "^10.9.2", - "typescript": "^5.4.5" + "typescript": "^5.4.5", + "typescript-eslint": "^7.9.0" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 076f5bb..bcf24f1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -52,6 +52,9 @@ dependencies: version: 3.22.4 devDependencies: + '@eslint/js': + specifier: ^9.2.0 + version: 9.2.0 '@swc-node/register': specifier: ^1.9.0 version: 1.9.0(@swc/core@1.4.16)(@swc/types@0.1.6)(typescript@5.4.5) @@ -112,6 +115,9 @@ devDependencies: typescript: specifier: ^5.4.5 version: 5.4.5 + typescript-eslint: + specifier: ^7.9.0 + version: 7.9.0(eslint@8.57.0)(typescript@5.4.5) packages: @@ -574,6 +580,11 @@ packages: engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} dev: true + /@eslint/js@9.2.0: + resolution: {integrity: sha512-ESiIudvhoYni+MdsI8oD7skpprZ89qKocwRM2KEvhhBJ9nl5MRh7BXU5GTod7Mdygq+AUl+QzId6iWJKR/wABA==} + engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} + dev: true + /@humanwhocodes/config-array@0.11.14: resolution: {integrity: sha512-3T8LkOmg45BV5FICb15QQMsyUSWrQ8AygVfC7ZG32zOalnqrilm018ZVCw0eapXux8FtA33q8PSRSstjee3jSg==} engines: {node: '>=10.10.0'} @@ -1170,6 +1181,33 @@ packages: '@types/send': 0.17.4 dev: true + /@typescript-eslint/eslint-plugin@7.9.0(@typescript-eslint/parser@7.9.0)(eslint@8.57.0)(typescript@5.4.5): + resolution: {integrity: sha512-6e+X0X3sFe/G/54aC3jt0txuMTURqLyekmEHViqyA2VnxhLMpvA6nqmcjIy+Cr9tLDHPssA74BP5Mx9HQIxBEA==} + engines: {node: ^18.18.0 || >=20.0.0} + peerDependencies: + '@typescript-eslint/parser': ^7.0.0 + eslint: ^8.56.0 + typescript: '*' + peerDependenciesMeta: + typescript: + optional: true + dependencies: + '@eslint-community/regexpp': 4.10.0 + '@typescript-eslint/parser': 7.9.0(eslint@8.57.0)(typescript@5.4.5) + '@typescript-eslint/scope-manager': 7.9.0 + '@typescript-eslint/type-utils': 7.9.0(eslint@8.57.0)(typescript@5.4.5) + '@typescript-eslint/utils': 7.9.0(eslint@8.57.0)(typescript@5.4.5) + '@typescript-eslint/visitor-keys': 7.9.0 + eslint: 8.57.0 + graphemer: 1.4.0 + ignore: 5.3.1 + natural-compare: 1.4.0 + ts-api-utils: 1.3.0(typescript@5.4.5) + typescript: 5.4.5 + transitivePeerDependencies: + - supports-color + dev: true + /@typescript-eslint/parser@7.2.0(eslint@8.57.0)(typescript@5.4.5): resolution: {integrity: sha512-5FKsVcHTk6TafQKQbuIVkXq58Fnbkd2wDL4LB7AURN7RUOu1utVP+G8+6u3ZhEroW3DF6hyo3ZEXxgKgp4KeCg==} engines: {node: ^16.0.0 || >=18.0.0} @@ -1191,6 +1229,27 @@ packages: - supports-color dev: true + /@typescript-eslint/parser@7.9.0(eslint@8.57.0)(typescript@5.4.5): + resolution: {integrity: sha512-qHMJfkL5qvgQB2aLvhUSXxbK7OLnDkwPzFalg458pxQgfxKDfT1ZDbHQM/I6mDIf/svlMkj21kzKuQ2ixJlatQ==} + engines: {node: ^18.18.0 || >=20.0.0} + peerDependencies: + eslint: ^8.56.0 + typescript: '*' + peerDependenciesMeta: + typescript: + optional: true + dependencies: + '@typescript-eslint/scope-manager': 7.9.0 + '@typescript-eslint/types': 7.9.0 + '@typescript-eslint/typescript-estree': 7.9.0(typescript@5.4.5) + '@typescript-eslint/visitor-keys': 7.9.0 + debug: 4.3.4(supports-color@5.5.0) + eslint: 8.57.0 + typescript: 5.4.5 + transitivePeerDependencies: + - supports-color + dev: true + /@typescript-eslint/scope-manager@7.2.0: resolution: {integrity: sha512-Qh976RbQM/fYtjx9hs4XkayYujB/aPwglw2choHmf3zBjB4qOywWSdt9+KLRdHubGcoSwBnXUH2sR3hkyaERRg==} engines: {node: ^16.0.0 || >=18.0.0} @@ -1199,11 +1258,44 @@ packages: '@typescript-eslint/visitor-keys': 7.2.0 dev: true + /@typescript-eslint/scope-manager@7.9.0: + resolution: {integrity: sha512-ZwPK4DeCDxr3GJltRz5iZejPFAAr4Wk3+2WIBaj1L5PYK5RgxExu/Y68FFVclN0y6GGwH8q+KgKRCvaTmFBbgQ==} + engines: {node: ^18.18.0 || >=20.0.0} + dependencies: + '@typescript-eslint/types': 7.9.0 + '@typescript-eslint/visitor-keys': 7.9.0 + dev: true + + /@typescript-eslint/type-utils@7.9.0(eslint@8.57.0)(typescript@5.4.5): + resolution: {integrity: sha512-6Qy8dfut0PFrFRAZsGzuLoM4hre4gjzWJB6sUvdunCYZsYemTkzZNwF1rnGea326PHPT3zn5Lmg32M/xfJfByA==} + engines: {node: ^18.18.0 || >=20.0.0} + peerDependencies: + eslint: ^8.56.0 + typescript: '*' + peerDependenciesMeta: + typescript: + optional: true + dependencies: + '@typescript-eslint/typescript-estree': 7.9.0(typescript@5.4.5) + '@typescript-eslint/utils': 7.9.0(eslint@8.57.0)(typescript@5.4.5) + debug: 4.3.4(supports-color@5.5.0) + eslint: 8.57.0 + ts-api-utils: 1.3.0(typescript@5.4.5) + typescript: 5.4.5 + transitivePeerDependencies: + - supports-color + dev: true + /@typescript-eslint/types@7.2.0: resolution: {integrity: sha512-XFtUHPI/abFhm4cbCDc5Ykc8npOKBSJePY3a3s+lwumt7XWJuzP5cZcfZ610MIPHjQjNsOLlYK8ASPaNG8UiyA==} engines: {node: ^16.0.0 || >=18.0.0} dev: true + /@typescript-eslint/types@7.9.0: + resolution: {integrity: sha512-oZQD9HEWQanl9UfsbGVcZ2cGaR0YT5476xfWE0oE5kQa2sNK2frxOlkeacLOTh9po4AlUT5rtkGyYM5kew0z5w==} + engines: {node: ^18.18.0 || >=20.0.0} + dev: true + /@typescript-eslint/typescript-estree@7.2.0(typescript@5.4.5): resolution: {integrity: sha512-cyxS5WQQCoBwSakpMrvMXuMDEbhOo9bNHHrNcEWis6XHx6KF518tkF1wBvKIn/tpq5ZpUYK7Bdklu8qY0MsFIA==} engines: {node: ^16.0.0 || >=18.0.0} @@ -1226,6 +1318,44 @@ packages: - supports-color dev: true + /@typescript-eslint/typescript-estree@7.9.0(typescript@5.4.5): + resolution: {integrity: sha512-zBCMCkrb2YjpKV3LA0ZJubtKCDxLttxfdGmwZvTqqWevUPN0FZvSI26FalGFFUZU/9YQK/A4xcQF9o/VVaCKAg==} + engines: {node: ^18.18.0 || >=20.0.0} + peerDependencies: + typescript: '*' + peerDependenciesMeta: + typescript: + optional: true + dependencies: + '@typescript-eslint/types': 7.9.0 + '@typescript-eslint/visitor-keys': 7.9.0 + debug: 4.3.4(supports-color@5.5.0) + globby: 11.1.0 + is-glob: 4.0.3 + minimatch: 9.0.4 + semver: 7.6.0 + ts-api-utils: 1.3.0(typescript@5.4.5) + typescript: 5.4.5 + transitivePeerDependencies: + - supports-color + dev: true + + /@typescript-eslint/utils@7.9.0(eslint@8.57.0)(typescript@5.4.5): + resolution: {integrity: sha512-5KVRQCzZajmT4Ep+NEgjXCvjuypVvYHUW7RHlXzNPuak2oWpVoD1jf5xCP0dPAuNIchjC7uQyvbdaSTFaLqSdA==} + engines: {node: ^18.18.0 || >=20.0.0} + peerDependencies: + eslint: ^8.56.0 + dependencies: + '@eslint-community/eslint-utils': 4.4.0(eslint@8.57.0) + '@typescript-eslint/scope-manager': 7.9.0 + '@typescript-eslint/types': 7.9.0 + '@typescript-eslint/typescript-estree': 7.9.0(typescript@5.4.5) + eslint: 8.57.0 + transitivePeerDependencies: + - supports-color + - typescript + dev: true + /@typescript-eslint/visitor-keys@7.2.0: resolution: {integrity: sha512-c6EIQRHhcpl6+tO8EMR+kjkkV+ugUNXOmeASA1rlzkd8EPIriavpWoiEz1HR/VLhbVIdhqnV6E7JZm00cBDx2A==} engines: {node: ^16.0.0 || >=18.0.0} @@ -1234,6 +1364,14 @@ packages: eslint-visitor-keys: 3.4.3 dev: true + /@typescript-eslint/visitor-keys@7.9.0: + resolution: {integrity: sha512-iESPx2TNLDNGQLyjKhUvIKprlP49XNEK+MvIf9nIO7ZZaZdbnfWKHnXAgufpxqfA0YryH8XToi4+CjBgVnFTSQ==} + engines: {node: ^18.18.0 || >=20.0.0} + dependencies: + '@typescript-eslint/types': 7.9.0 + eslint-visitor-keys: 3.4.3 + dev: true + /@ungap/structured-clone@1.2.0: resolution: {integrity: sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==} dev: true @@ -2450,7 +2588,7 @@ packages: eslint: 8.57.0 eslint-import-resolver-node: 0.3.9 eslint-import-resolver-typescript: 3.6.1(@typescript-eslint/parser@7.2.0)(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.29.1)(eslint@8.57.0) - eslint-plugin-import: 2.29.1(@typescript-eslint/parser@7.2.0)(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0) + eslint-plugin-import: 2.29.1(@typescript-eslint/parser@7.9.0)(eslint@8.57.0) eslint-plugin-jsx-a11y: 6.8.0(eslint@8.57.0) eslint-plugin-react: 7.34.1(eslint@8.57.0) eslint-plugin-react-hooks: 4.6.2(eslint@8.57.0) @@ -2490,7 +2628,7 @@ packages: enhanced-resolve: 5.16.1 eslint: 8.57.0 eslint-module-utils: 2.8.1(@typescript-eslint/parser@7.2.0)(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0) - eslint-plugin-import: 2.29.1(@typescript-eslint/parser@7.2.0)(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0) + eslint-plugin-import: 2.29.1(@typescript-eslint/parser@7.9.0)(eslint@8.57.0) fast-glob: 3.3.2 get-tsconfig: 4.7.3 is-core-module: 2.13.1 @@ -2532,7 +2670,36 @@ packages: - supports-color dev: true - /eslint-plugin-import@2.29.1(@typescript-eslint/parser@7.2.0)(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0): + /eslint-module-utils@2.8.1(@typescript-eslint/parser@7.9.0)(eslint-import-resolver-node@0.3.9)(eslint@8.57.0): + resolution: {integrity: sha512-rXDXR3h7cs7dy9RNpUlQf80nX31XWJEyGq1tRMo+6GsO5VmTe4UTwtmonAD4ZkAsrfMVDA2wlGJ3790Ys+D49Q==} + engines: {node: '>=4'} + peerDependencies: + '@typescript-eslint/parser': '*' + eslint: '*' + eslint-import-resolver-node: '*' + eslint-import-resolver-typescript: '*' + eslint-import-resolver-webpack: '*' + peerDependenciesMeta: + '@typescript-eslint/parser': + optional: true + eslint: + optional: true + eslint-import-resolver-node: + optional: true + eslint-import-resolver-typescript: + optional: true + eslint-import-resolver-webpack: + optional: true + dependencies: + '@typescript-eslint/parser': 7.9.0(eslint@8.57.0)(typescript@5.4.5) + debug: 3.2.7 + eslint: 8.57.0 + eslint-import-resolver-node: 0.3.9 + transitivePeerDependencies: + - supports-color + dev: true + + /eslint-plugin-import@2.29.1(@typescript-eslint/parser@7.9.0)(eslint@8.57.0): resolution: {integrity: sha512-BbPC0cuExzhiMo4Ff1BTVwHpjjv28C5R+btTOGaCRC7UEz801up0JadwkeSk5Ued6TG34uaczuVuH6qyy5YUxw==} engines: {node: '>=4'} peerDependencies: @@ -2542,7 +2709,7 @@ packages: '@typescript-eslint/parser': optional: true dependencies: - '@typescript-eslint/parser': 7.2.0(eslint@8.57.0)(typescript@5.4.5) + '@typescript-eslint/parser': 7.9.0(eslint@8.57.0)(typescript@5.4.5) array-includes: 3.1.8 array.prototype.findlastindex: 1.2.5 array.prototype.flat: 1.3.2 @@ -2551,7 +2718,7 @@ packages: doctrine: 2.1.0 eslint: 8.57.0 eslint-import-resolver-node: 0.3.9 - eslint-module-utils: 2.8.1(@typescript-eslint/parser@7.2.0)(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0) + eslint-module-utils: 2.8.1(@typescript-eslint/parser@7.9.0)(eslint-import-resolver-node@0.3.9)(eslint@8.57.0) hasown: 2.0.2 is-core-module: 2.13.1 is-glob: 4.0.3 @@ -5458,6 +5625,25 @@ packages: possible-typed-array-names: 1.0.0 dev: true + /typescript-eslint@7.9.0(eslint@8.57.0)(typescript@5.4.5): + resolution: {integrity: sha512-7iTn9c10teHHCys5Ud/yaJntXZrjt3h2mrx3feJGBOLgQkF3TB1X89Xs3aVQ/GgdXRAXpk2bPTdpRwHP4YkUow==} + engines: {node: ^18.18.0 || >=20.0.0} + peerDependencies: + eslint: ^8.56.0 + typescript: '*' + peerDependenciesMeta: + typescript: + optional: true + dependencies: + '@typescript-eslint/eslint-plugin': 7.9.0(@typescript-eslint/parser@7.9.0)(eslint@8.57.0)(typescript@5.4.5) + '@typescript-eslint/parser': 7.9.0(eslint@8.57.0)(typescript@5.4.5) + '@typescript-eslint/utils': 7.9.0(eslint@8.57.0)(typescript@5.4.5) + eslint: 8.57.0 + typescript: 5.4.5 + transitivePeerDependencies: + - supports-color + dev: true + /typescript@5.4.5: resolution: {integrity: sha512-vcI4UpRgg81oIRUFwR0WSIHKt11nJ7SAVlYNIu+QpqeyXP+gpQJy/Z4+F0aGxSE4MqwjyXvW/TzgkLAx2AGHwQ==} engines: {node: '>=14.17'} diff --git a/src/minio.ts b/src/minio.ts index d00f040..77bcca8 100644 --- a/src/minio.ts +++ b/src/minio.ts @@ -18,16 +18,16 @@ export async function createUploadImageUrl( objectName: string, contentType: string, ) { - let policy = mc.newPostPolicy(); + const policy = mc.newPostPolicy(); policy.setKey(objectName); policy.setBucket(Config.bucketName); - let expires = new Date(); + const expires = new Date(); expires.setSeconds(30 * 60); policy.setExpires(expires); policy.setContentType(contentType); policy.setContentDisposition(`attachment; filename="${objectName}"`); policy.setContentLengthRange(1, 3 * 1024 * 1024); - let rs = await mc.presignedPostPolicy(policy); + const rs = await mc.presignedPostPolicy(policy); return rs; } diff --git a/src/trpc.ts b/src/trpc.ts index 2c0c428..81c6dff 100644 --- a/src/trpc.ts +++ b/src/trpc.ts @@ -42,7 +42,7 @@ export const createContext = async (opts: CreateHTTPContextOptions) => { const bearerToken = authorizationHeader.split(" ")[1]; const phone = await verifyToken(bearerToken); if (phone !== null) { - let user = await db.query.user.findFirst({ + const user = await db.query.user.findFirst({ where: (user, { eq }) => eq(user.phone, phone), }); return { @@ -59,7 +59,7 @@ export const createContext = async (opts: CreateHTTPContextOptions) => { async function verifyToken(token: string): Promise { try { - let rs = await new Promise((resolve, reject) => { + const rs = await new Promise((resolve, reject) => { jwt.verify(token, Config.jwt_secret, (err, decoded) => { if (err) { reject(err); @@ -68,7 +68,7 @@ async function verifyToken(token: string): Promise { } }); }); - let data = z + const data = z .object({ phone: z.string(), }) diff --git a/src/userRoute.ts b/src/userRoute.ts index 544db66..bd5f830 100644 --- a/src/userRoute.ts +++ b/src/userRoute.ts @@ -1,13 +1,6 @@ import { router, publicProcedure, protectedProcedure } from "./trpc"; import { db } from "./db"; -import { - imageToUser, - opinion, - province, - user, - userOpinion, - zone, -} from "./schema"; +import { imageToUser, opinion, user, userOpinion, zone } from "./schema"; import { createInsertSchema } from "drizzle-zod"; import { z } from "zod"; import { SQL, and, count, eq, inArray, sql } from "drizzle-orm"; @@ -125,11 +118,11 @@ async function getAllUserCount( zoneId?: number, provinceId?: number, ) { - let zoneIds: number[] = await getZone(provinceId); + const zoneIds: number[] = await getZone(provinceId); if (provinceId && zoneIds.length === 0) { return []; } - let conditions: SQL[] = []; + const conditions: SQL[] = []; if (group !== undefined) { conditions.push(eq(user.group, group)); } @@ -154,11 +147,11 @@ async function getAllUser( zoneId?: number, provinceId?: number, ) { - let zoneIds: number[] = await getZone(provinceId); + const zoneIds: number[] = await getZone(provinceId); if (provinceId && zoneIds.length === 0) { return []; } - let users = await db.query.user.findMany({ + const users = await db.query.user.findMany({ with: { group: true, opinions: { @@ -172,7 +165,7 @@ async function getAllUser( offset, orderBy: sql`random()`, where: (user, { eq, and }) => { - let conditions: SQL[] = []; + const conditions: SQL[] = []; if (group !== undefined) { conditions.push(eq(user.group, group)); } @@ -195,7 +188,7 @@ async function getAllUser( } async function getUser(userId: number, showPhone: boolean) { - let user = await db.query.user.findFirst({ + const user = await db.query.user.findFirst({ where: (user, { eq }) => eq(user.id, userId), with: { group: true, @@ -224,10 +217,10 @@ async function createUser( opinions: OpinionInsertSchema, ) { try { - let result = ( + const result = ( await db.insert(user).values(newUser).returning({ id: user.id }) )[0]; - for (let op of opinions) { + for (const op of opinions) { await db.insert(userOpinion).values({ ...op, userId: result.id }); } return { token: createJWT(newUser.phone) }; @@ -248,7 +241,7 @@ async function updateUser(userId: number, update: UserUpdateSchema) { } async function login(cid: string, phone: string) { - let user = await db.query.user.findFirst({ + const user = await db.query.user.findFirst({ where: (user, { and, eq }) => and(eq(user.cid, cid), eq(user.phone, phone)), }); if (user === undefined) { @@ -267,7 +260,7 @@ async function changeOpinion( opinionChoice: OpinionInsertSchema[0]["choice"], ) { try { - let thisOpinion = await db + const thisOpinion = await db .select() .from(opinion) .where(eq(opinion.id, opinionId)) @@ -328,11 +321,11 @@ async function requestChangeImage( }); } // Create a unique image name - let tryCount = 0; + const tryCount = 0; let objectName: string | null = null; while (tryCount < 3) { - let imageName = `${generateRandomString()}.${extension}`; - let ok = await db + const imageName = `${generateRandomString()}.${extension}`; + const ok = await db .select({ value: count(user.image) }) .from(user) .where(eq(user.image, imageName)) @@ -356,14 +349,14 @@ async function requestChangeImage( target: [imageToUser.userId], set: { imageName: objectName }, }); - let request = await createUploadImageUrl(mc, objectName, contentType); + const request = await createUploadImageUrl(mc, objectName, contentType); request.postURL = Config.minioPublicBucketEndpoint; return request; } async function confirmChangeImage(userId: number, oldImage: string | null) { const mc = createClient(); - let rs = await db + const rs = await db .select({ imageName: imageToUser.imageName }) .from(imageToUser) .where(eq(imageToUser.userId, userId)); @@ -373,7 +366,7 @@ async function confirmChangeImage(userId: number, oldImage: string | null) { code: "BAD_REQUEST", }); } - let imageName = rs[0].imageName; + const imageName = rs[0].imageName; const isImageExist = await mc .statObject(Config.bucketName, imageName) .then(() => true) @@ -384,7 +377,7 @@ async function confirmChangeImage(userId: number, oldImage: string | null) { code: "BAD_REQUEST", }); } - const promises: Promise[] = []; + const promises: Promise[] = []; if (oldImage) { promises.push(mc.removeObject(Config.bucketName, oldImage).catch(() => {})); }