{"version":3,"file":"credentials.mjs","names":["e: any"],"sources":["../../../src/http/endpoints/credentials.ts"],"sourcesContent":["import type { Router, Response } from 'express'\nimport type { OpenBadgesHttpModuleConfig } from '../OpenBadgesHttpModuleConfig'\nimport type { OpenBadgesHttpModule } from '../OpenBadgesHttpModule'\nimport type { ObRequest } from '../router'\n\nimport { bearerAuth, requireScopes } from '../middleware/auth'\nimport { getRequestContext, sendError, sendJson } from '../router'\nimport { OpenBadgeCredentialRepository } from '../../repository/OpenBadgeCredentialRepository'\nimport { OpenBadgeCredentialRecord } from '../../repository/OpenBadgeCredentialRecord'\nimport { DisplayMapper } from '../../services/DisplayMapper'\n\nconst OB_READ = 'https://purl.imsglobal.org/spec/ob/v3p0/scope/readonly'\nconst OB_WRITE = 'https://purl.imsglobal.org/spec/ob/v3p0/scope/replace'\nconst OB_DELETE = 'https://purl.imsglobal.org/spec/ob/v3p0/scope/delete'\n\nexport function configureCredentialsEndpoints(router: Router, config: OpenBadgesHttpModuleConfig, module: OpenBadgesHttpModule) {\n  // POST create/upsert\n  router.post(config.credentialsPath, bearerAuth(module), requireScopes([OB_WRITE]), async (req: ObRequest, res: Response) => {\n    try {\n      const { agentContext } = getRequestContext(req)\n      const auth = (req.requestContext as any).auth\n      const body = req.body\n      if (!body || typeof body !== 'object') return sendError(res, 400, 'invalid_request', 'Missing credential body')\n\n      const repo = agentContext.dependencyManager.resolve(OpenBadgeCredentialRepository)\n      const mapper = agentContext.dependencyManager.resolve(DisplayMapper)\n\n      const record = new OpenBadgeCredentialRecord({\n        credential: body,\n        status: 'unknown',\n        derived: mapper.toDisplay(body),\n        tags: { subjectId: auth.subject },\n      })\n      await repo.save(agentContext, record)\n\n      return sendJson(res, { id: record.id }, 201)\n    } catch (e: any) {\n      return sendError(res, 500, 'server_error', e?.message || 'Unexpected error')\n    }\n  })\n\n  // GET list\n  router.get(config.credentialsPath, bearerAuth(module), requireScopes([OB_READ]), async (req: ObRequest, res: Response) => {\n    try {\n      const { agentContext } = getRequestContext(req)\n      const auth = (req.requestContext as any).auth\n      const repo = agentContext.dependencyManager.resolve(OpenBadgeCredentialRepository)\n      const items = await repo.findByQuery(agentContext, { subjectId: auth.subject })\n      return sendJson(res, items.map((r: any) => ({ id: r.id, status: r.status, derived: r.derived })))\n    } catch (e: any) {\n      return sendError(res, 500, 'server_error', e?.message || 'Unexpected error')\n    }\n  })\n\n  // GET one\n  router.get(`${config.credentialsPath}/:id`, bearerAuth(module), requireScopes([OB_READ]), async (req: ObRequest, res: Response) => {\n    try {\n      const { agentContext } = getRequestContext(req)\n      const repo = agentContext.dependencyManager.resolve(OpenBadgeCredentialRepository)\n      const id = String(req.params.id)\n      const record = await repo.getById(agentContext, id)\n      if (!record) return sendError(res, 404, 'not_found', 'Credential not found')\n      return sendJson(res, record.credential)\n    } catch (e: any) {\n      return sendError(res, 500, 'server_error', e?.message || 'Unexpected error')\n    }\n  })\n\n  // DELETE one\n  router.delete(\n    `${config.credentialsPath}/:id`,\n    bearerAuth(module),\n    requireScopes([OB_DELETE]),\n    async (req: ObRequest, res: Response) => {\n      try {\n        const { agentContext } = getRequestContext(req)\n        const auth = (req.requestContext as any).auth\n        const repo = agentContext.dependencyManager.resolve(OpenBadgeCredentialRepository)\n        const id = String(req.params.id)\n        const record = await repo.getById(agentContext, id)\n        if (!record) return sendError(res, 404, 'not_found', 'Credential not found')\n        const tags = record.getTags() as any\n        if (tags.subjectId && tags.subjectId !== auth.subject) return sendError(res, 404, 'not_found', 'Credential not found')\n        await repo.deleteById(agentContext, id)\n        return sendJson(res, { deleted: true })\n      } catch (e: any) {\n        return sendError(res, 500, 'server_error', e?.message || 'Unexpected error')\n      }\n    }\n  )\n}\n"],"mappings":";;;;;;;aAMkE;AAKlE,MAAM,UAAU;AAChB,MAAM,WAAW;AACjB,MAAM,YAAY;AAElB,SAAgB,8BAA8B,QAAgB,QAAoC,QAA8B;AAE9H,QAAO,KAAK,OAAO,iBAAiB,WAAW,OAAO,EAAE,cAAc,CAAC,SAA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