oauth2_flow.ts (1363B)
1 import { OAuth2FlowRepository } from "#core/application/oauth2/flow_repository.ts"; 2 import { 3 EntityLocked, 4 EntityNotFound, 5 } from "#core/application/repository_error.ts"; 6 import { OAuth2Flow } from "#core/domain/oauth2flow.ts"; 7 import { Token } from "#core/domain/token.ts"; 8 import { UUID } from "#core/domain/uuid.ts"; 9 import { 10 mapFromOAuth2Flow, 11 mapToOAuth2Flow, 12 OAuth2FlowDto, 13 } from "./mapper/oauth2flow.ts"; 14 15 export class MemoryOAuth2FlowRepositoryAdapter implements OAuth2FlowRepository { 16 constructor(private readonly entities: Map<string, OAuth2FlowDto>) { 17 } 18 19 find(id: UUID): OAuth2Flow { 20 const entity = this.entities.get(id.toString()); 21 if (entity !== undefined) { 22 return mapToOAuth2Flow(entity); 23 } 24 throw new EntityNotFound(id.toString()); 25 } 26 27 findByToken(token: Token): OAuth2Flow { 28 const entity = Array.from(this.entities.values()) 29 .find((item) => item.token === token.toString()); 30 if (entity !== undefined) { 31 return mapToOAuth2Flow(entity); 32 } 33 throw new EntityNotFound(token.toString()); 34 } 35 36 store(entity: OAuth2Flow): void { 37 const dto = mapFromOAuth2Flow(entity); 38 const latest = this.entities.get(dto.uuid)?.version ?? 0; 39 if (latest > dto.version) { 40 throw new EntityLocked(); 41 } 42 entity.version = ++dto.version; 43 this.entities.set(dto.uuid, dto); 44 } 45 }