@@ -7,25 +7,18 @@ import { NextFunction, Response, Router } from 'express';
77import asyncHandler from 'express-async-handler' ;
88
99import { jsonError } from '../../middleware' ;
10- import { CreateError , ErrorHelper , getProviders } from '../../transitional' ;
10+ import { CreateError , getProviders } from '../../transitional' ;
1111import { ReposAppRequest } from '../../interfaces' ;
1212
1313import RouteOrganization from './organization' ;
14- import {
15- IReposAppRequestWithOrganizationManagementType ,
16- OrganizationManagementType ,
17- } from '../../middleware/business/organization' ;
18-
14+ import { apiMiddlewareOrganizationsToOrganization } from '../../middleware/business/organization' ;
1915import type { GitHubOrganizationResponseSanitized } from '../../business' ;
2016import {
2117 OrganizationAnnotation ,
2218 OrganizationAnnotationProperty ,
2319 scrubOrganizationAnnotation ,
2420} from '../../entities/organizationAnnotation' ;
25- import {
26- getOrganizationProfileViaMemoryCache ,
27- setOrganizationProfileForRequest ,
28- } from '../../middleware/github/ensureOrganizationProfile' ;
21+ import { getOrganizationProfileViaMemoryCache } from '../../middleware/github/ensureOrganizationProfile' ;
2922
3023const router : Router = Router ( ) ;
3124
@@ -128,46 +121,10 @@ router.get(
128121 } )
129122) ;
130123
131- router . use (
132- '/:orgName' ,
133- asyncHandler (
134- async ( req : IReposAppRequestWithOrganizationManagementType , res : Response , next : NextFunction ) => {
135- const { operations } = getProviders ( req ) ;
136- const { orgName } = req . params ;
137- req . organizationName = orgName ;
138- try {
139- const org = operations . getOrganization ( orgName ) ;
140- if ( org ) {
141- req . organizationManagementType = OrganizationManagementType . Managed ;
142- req . organization = org ;
143- return next ( ) ;
144- }
145- } catch ( orgNotFoundError ) {
146- if ( ! ErrorHelper . IsNotFound ( orgNotFoundError ) ) {
147- return next ( orgNotFoundError ) ;
148- }
149- }
150- try {
151- const org = operations . getUncontrolledOrganization ( orgName ) ;
152- req . organizationManagementType = OrganizationManagementType . Unmanaged ;
153- req . organization = org ;
154- await setOrganizationProfileForRequest ( req ) ;
155- } catch ( orgProfileError ) {
156- if ( ErrorHelper . IsNotFound ( orgProfileError ) ) {
157- return next ( CreateError . NotFound ( `The organization ${ orgName } does not exist` ) ) ;
158- } else {
159- return next ( orgProfileError ) ;
160- }
161- }
162- return next ( ) ;
163- }
164- )
165- ) ;
166-
167- router . use ( '/:orgName' , RouteOrganization ) ;
124+ router . use ( '/:orgName' , asyncHandler ( apiMiddlewareOrganizationsToOrganization ) , RouteOrganization ) ;
168125
169126router . use ( '*' , ( req : ReposAppRequest , res : Response , next : NextFunction ) => {
170- return next ( jsonError ( 'orgs API not found' , 404 ) ) ;
127+ return next ( CreateError . NotFound ( 'orgs API not found' ) ) ;
171128} ) ;
172129
173130export default router ;
0 commit comments