{"size": 2253, "ext": "js", "max_stars_repo_path": "api/autocomplete/dao.js", "max_stars_repo_name": "EsmeMedina/openopps-platform", "max_stars_repo_stars_event_min_datetime": "2019-11-08T15:21:15.000Z", "max_stars_repo_stars_event_max_datetime": "2021-07-05T01:28:57.000Z", "max_issues_repo_path": "api/autocomplete/dao.js", "max_issues_repo_name": "EsmeMedina/openopps-platform", "max_issues_repo_issues_event_min_datetime": "2020-04-16T20:39:47.000Z", "max_issues_repo_issues_event_max_datetime": "2021-07-09T22:51:42.000Z", "max_forks_repo_path": "api/autocomplete/dao.js", "max_forks_repo_name": "EsmeMedina/openopps-platform", "max_forks_repo_forks_event_min_datetime": "2020-11-09T21:02:09.000Z", "max_forks_repo_forks_event_max_datetime": "2020-11-25T18:21:33.000Z"}
{"max_stars_count": 2, "max_issues_count": 21, "max_forks_count": 3, "avg_line_length": 42.5094339623, "max_line_length": 191, "alphanum_fraction": 0.7172658677}
const _ = require('lodash');
var dao = require('postgres-gen-dao');

const tagByType = 'select @tagentity.* from @tagentity tagentity where tagentity.type = ?';

const userByName = 'select midas_user.id, midas_user.name, midas_user.title, midas_user.username, midas_user.government_uri ' +
  'from midas_user where disabled = false and hiring_path in (\'fed\', \'contractor\') and LOWER(name) like ?';

const agencyQuery = 'select * from agency where is_disabled = false and parent_code is not null and (lower(name) like ? or lower(abbr) like ?)';

const languageQuery=' select language.language_id,language.value from language where LOWER(value) like ? and language.is_disabled = false';

const countryQuery='select country.country_id,country.code,country.value from country where LOWER(value) like ?';

const stateQuery='select country_subdivision.country_subdivision_id,country_subdivision.code,country_subdivision.value from country_subdivision where LOWER(value) like ? and parent_code = ?';

const agencyAutocompleteQuery = `select agency_id as id, name, abbr from (
	select ROW_NUMBER() over (order by agency_id), agency_id, name, abbr from agency where lower(abbr) like ? and parent_code is not null
	union select 5 + ROW_NUMBER() over (order by agency_id), agency_id, name, abbr from agency where lower("name") like ? and parent_code is not null
) a order by row_number limit 5`;

const tagAutocompleteQuery = 'select id, name from tagentity where type = ? and lower(name) like ? limit 5';

const options = {
  tagByType: {
    exclude: {
      tagentity: [ 'deletedAt' ],
    },
  },
};

module.exports = function (db) {
  return {
    Agency: dao({ db: db, table: 'agency' }),
    TagEntity: dao({ db: db, table: 'tagentity' }),
    User: dao({ db: db, table: 'midas_user' }),
    Language:dao({db:db,table:'language'}),
    Country:dao({db:db,table:'country'}),
    CountrySubdivision:dao({db:db,table:'country_subdivision'}),
    query: {
      tagByType: tagByType,
      userByName: userByName,
      agency: agencyQuery,
      language:languageQuery,
      country:countryQuery,
      state: stateQuery,
      agencyAutocomplete: agencyAutocompleteQuery,
      tagAutocomplete: tagAutocompleteQuery,
    },
    options: options,
  };
};
