{"version":3,"file":"create-single.mjs","names":[],"sources":["../../../src/services/user-tokens/create-single.ts"],"sourcesContent":["import crypto from \"node:crypto\";\nimport type { UserTokenType } from \"../../libs/db/types.js\";\nimport { UserTokensRepository } from \"../../libs/repositories/index.js\";\nimport hashUserToken from \"../../utils/helpers/hash-user-token.js\";\nimport type { ServiceFn } from \"../../utils/services/types.js\";\n\nconst createSingle: ServiceFn<\n\t[\n\t\t{\n\t\t\tuserId: number;\n\t\t\ttokenType: UserTokenType;\n\t\t\texpiryDate: string;\n\t\t},\n\t],\n\t{\n\t\ttoken: string;\n\t\ttokenId: number;\n\t}\n> = async (context, data) => {\n\tconst UserTokens = new UserTokensRepository(\n\t\tcontext.db.client,\n\t\tcontext.config.db,\n\t);\n\n\tconst token = crypto.randomBytes(32).toString(\"hex\");\n\tconst hashedToken = hashUserToken(token);\n\n\tconst userTokenRes = await UserTokens.createSingle({\n\t\tdata: {\n\t\t\tuser_id: data.userId,\n\t\t\ttoken_type: data.tokenType,\n\t\t\texpiry_date: data.expiryDate,\n\t\t\ttoken: hashedToken,\n\t\t},\n\t\treturning: [\"id\"],\n\t\tvalidation: {\n\t\t\tenabled: true,\n\t\t},\n\t});\n\tif (userTokenRes.error) return userTokenRes;\n\n\treturn {\n\t\terror: undefined,\n\t\tdata: {\n\t\t\ttoken: token,\n\t\t\ttokenId: userTokenRes.data.id,\n\t\t},\n\t};\n};\n\nexport default createSingle;\n"],"mappings":"yIAMA,MAAM,EAYF,MAAO,EAAS,IAAS,CAC5B,IAAM,EAAa,IAAI,EACtB,EAAQ,GAAG,OACX,EAAQ,OAAO,EAChB,EAEM,EAAQ,EAAO,YAAY,EAAE,CAAC,CAAC,SAAS,KAAK,EAC7C,EAAc,EAAc,CAAK,EAEjC,EAAe,MAAM,EAAW,aAAa,CAClD,KAAM,CACL,QAAS,EAAK,OACd,WAAY,EAAK,UACjB,YAAa,EAAK,WAClB,MAAO,CACR,EACA,UAAW,CAAC,IAAI,EAChB,WAAY,CACX,QAAS,EACV,CACD,CAAC,EAGD,OAFI,EAAa,MAAc,EAExB,CACN,MAAO,IAAA,GACP,KAAM,CACE,QACP,QAAS,EAAa,KAAK,EAC5B,CACD,CACD"}