chore: migrating to nodejs 22

This commit is contained in:
Rafael Taranto 2025-03-08 12:28:23 +00:00
parent 0296f86060
commit 2e31ab391f
47 changed files with 16384 additions and 11137 deletions

View file

@ -1,40 +1,49 @@
const _ = require('lodash/fp')
const { SchemaDirectiveVisitor, AuthenticationError } = require('apollo-server-express')
const { mapSchema, getDirective, MapperKind } = require('@graphql-tools/utils')
const { defaultFieldResolver } = require('graphql')
class AuthDirective extends SchemaDirectiveVisitor {
visitObject (type) {
this.ensureFieldsWrapped(type)
type._requiredAuthRole = this.args.requires
}
const { AuthenticationError } = require('../errors')
visitFieldDefinition (field, details) {
this.ensureFieldsWrapped(details.objectType)
field._requiredAuthRole = this.args.requires
}
ensureFieldsWrapped (objectType) {
if (objectType._authFieldsWrapped) return
objectType._authFieldsWrapped = true
const fields = objectType.getFields()
_.forEach(fieldName => {
const field = fields[fieldName]
const { resolve = defaultFieldResolver } = field
field.resolve = function (root, args, context, info) {
const requiredRoles = field._requiredAuthRole ? field._requiredAuthRole : objectType._requiredAuthRole
function authDirectiveTransformer(schema, directiveName = 'auth') {
return mapSchema(schema, {
// For object types
[MapperKind.OBJECT_TYPE]: (objectType) => {
const directive = getDirective(schema, objectType, directiveName)?.[0]
if (directive) {
const requiredAuthRole = directive.requires
objectType._requiredAuthRole = requiredAuthRole
}
return objectType
},
// For field definitions
[MapperKind.OBJECT_FIELD]: (fieldConfig, _fieldName, typeName) => {
const directive = getDirective(schema, fieldConfig, directiveName)?.[0]
if (directive) {
const requiredAuthRole = directive.requires
fieldConfig._requiredAuthRole = requiredAuthRole
}
// Get the parent object type
const objectType = schema.getType(typeName)
// Apply auth check to the field's resolver
const { resolve = defaultFieldResolver } = fieldConfig
fieldConfig.resolve = function (root, args, context, info) {
const requiredRoles = fieldConfig._requiredAuthRole || objectType._requiredAuthRole
if (!requiredRoles) return resolve.apply(this, [root, args, context, info])
const user = context.req.session.user
if (!user || !_.includes(_.upperCase(user.role), requiredRoles)) throw new AuthenticationError('You do not have permission to access this resource!')
if (!user || !_.includes(_.upperCase(user.role), requiredRoles)) {
throw new AuthenticationError('You do not have permission to access this resource!')
}
return resolve.apply(this, [root, args, context, info])
}
}, _.keys(fields))
}
return fieldConfig
}
})
}
module.exports = AuthDirective
module.exports = authDirectiveTransformer

View file

@ -1,3 +1,3 @@
const AuthDirective = require('./auth')
const authDirectiveTransformer = require('./auth')
module.exports = { AuthDirective }
module.exports = { authDirectiveTransformer }

View file

@ -0,0 +1,71 @@
const { GraphQLError } = require('graphql')
const { ApolloServerErrorCode } = require('@apollo/server/errors')
class AuthenticationError extends GraphQLError {
constructor() {
super('Authentication failed', {
extensions: {
code: 'UNAUTHENTICATED'
}
})
}
}
class InvalidCredentialsError extends GraphQLError {
constructor() {
super('Invalid credentials', {
extensions: {
code: 'INVALID_CREDENTIALS'
}
})
}
}
class UserAlreadyExistsError extends GraphQLError {
constructor() {
super('User already exists', {
extensions: {
code: 'USER_ALREADY_EXISTS'
}
})
}
}
class InvalidTwoFactorError extends GraphQLError {
constructor() {
super('Invalid two-factor code', {
extensions: {
code: 'INVALID_TWO_FACTOR_CODE'
}
})
}
}
class InvalidUrlError extends GraphQLError {
constructor() {
super('Invalid URL token', {
extensions: {
code: 'INVALID_URL_TOKEN'
}
})
}
}
class UserInputError extends GraphQLError {
constructor() {
super('User input error', {
extensions: {
code: ApolloServerErrorCode.BAD_USER_INPUT
}
})
}
}
module.exports = {
AuthenticationError,
InvalidCredentialsError,
UserAlreadyExistsError,
InvalidTwoFactorError,
InvalidUrlError,
UserInputError
}

View file

@ -1,37 +0,0 @@
const { ApolloError, AuthenticationError } = require('apollo-server-express')
class InvalidCredentialsError extends ApolloError {
constructor(message) {
super(message, 'INVALID_CREDENTIALS')
Object.defineProperty(this, 'name', { value: 'InvalidCredentialsError' })
}
}
class UserAlreadyExistsError extends ApolloError {
constructor(message) {
super(message, 'USER_ALREADY_EXISTS')
Object.defineProperty(this, 'name', { value: 'UserAlreadyExistsError' })
}
}
class InvalidTwoFactorError extends ApolloError {
constructor(message) {
super(message, 'INVALID_TWO_FACTOR_CODE')
Object.defineProperty(this, 'name', { value: 'InvalidTwoFactorError' })
}
}
class InvalidUrlError extends ApolloError {
constructor(message) {
super(message, 'INVALID_URL_TOKEN')
Object.defineProperty(this, 'name', { value: 'InvalidUrlError' })
}
}
module.exports = {
AuthenticationError,
InvalidCredentialsError,
UserAlreadyExistsError,
InvalidTwoFactorError,
InvalidUrlError
}

View file

@ -8,7 +8,7 @@ const loginHelper = require('../../services/login')
const T = require('../../../time')
const users = require('../../../users')
const sessionManager = require('../../../session-manager')
const authErrors = require('../errors/authentication')
const authErrors = require('../errors')
const credentials = require('../../../hardware-credentials')
const REMEMBER_ME_AGE = 90 * T.day

View file

@ -1,13 +1,9 @@
const { GraphQLDateTime } = require('graphql-iso-date')
const { GraphQLJSON, GraphQLJSONObject } = require('graphql-type-json')
const { GraphQLUpload } = require('graphql-upload')
GraphQLDateTime.name = 'Date'
const { DateTimeISOResolver, JSONResolver, JSONObjectResolver } = require('graphql-scalars')
const resolvers = {
JSON: GraphQLJSON,
JSONObject: GraphQLJSONObject,
Date: GraphQLDateTime,
UploadGQL: GraphQLUpload
JSON: JSONResolver,
JSONObject: JSONObjectResolver,
Date: DateTimeISOResolver
}
module.exports = resolvers

View file

@ -1,4 +1,4 @@
const { gql } = require('apollo-server-express')
const gql = require('graphql-tag')
const typeDef = gql`
type Bill {

View file

@ -1,4 +1,4 @@
const { gql } = require('apollo-server-express')
const gql = require('graphql-tag')
const typeDef = gql`
type Blacklist {

View file

@ -1,4 +1,4 @@
const { gql } = require('apollo-server-express')
const gql = require('graphql-tag')
const typeDef = gql`
type CashboxBatch {

View file

@ -1,4 +1,4 @@
const { gql } = require('apollo-server-express')
const gql = require('graphql-tag')
const typeDef = gql`
type Country {

View file

@ -1,4 +1,4 @@
const { gql } = require('apollo-server-express')
const gql = require('graphql-tag')
const typeDef = gql`
type Currency {

View file

@ -1,4 +1,4 @@
const { gql } = require('apollo-server-express')
const gql = require('graphql-tag')
const typeDef = gql`

View file

@ -1,4 +1,4 @@
const { gql } = require('apollo-server-express')
const gql = require('graphql-tag')
const typeDef = gql`
type Customer {
@ -6,7 +6,7 @@ const typeDef = gql`
authorizedOverride: String
daysSuspended: Int
isSuspended: Boolean
newPhoto: UploadGQL
newPhoto: Upload
photoType: String
frontCameraPath: String
frontCameraAt: Date
@ -18,7 +18,7 @@ const typeDef = gql`
idCardData: JSONObject
idCardDataOverride: String
idCardDataExpiration: Date
idCardPhoto: UploadGQL
idCardPhoto: Upload
idCardPhotoPath: String
idCardPhotoOverride: String
idCardPhotoAt: Date
@ -74,7 +74,7 @@ const typeDef = gql`
input CustomerEdit {
idCardData: JSONObject
idCardPhoto: UploadGQL
idCardPhoto: Upload
usSsn: String
subscriberInfo: JSONObject
}
@ -108,7 +108,7 @@ const typeDef = gql`
removeCustomField(customerId: ID!, fieldId: ID!): Boolean @auth
editCustomer(customerId: ID!, customerEdit: CustomerEdit): Customer @auth
deleteEditedData(customerId: ID!, customerEdit: CustomerEdit): Customer @auth
replacePhoto(customerId: ID!, photoType: String, newPhoto: UploadGQL): Customer @auth
replacePhoto(customerId: ID!, photoType: String, newPhoto: Upload): Customer @auth
createCustomerNote(customerId: ID!, title: String!, content: String!): Boolean @auth
editCustomerNote(noteId: ID!, newContent: String!): Boolean @auth
deleteCustomerNote(noteId: ID!): Boolean @auth

View file

@ -1,4 +1,4 @@
const { gql } = require('apollo-server-express')
const gql = require('graphql-tag')
const typeDef = gql`
type CoinFunds {

View file

@ -1,4 +1,4 @@
const { gql } = require('apollo-server-express')
const gql = require('graphql-tag')
const typeDef = gql`
type MachineLog {

View file

@ -1,4 +1,4 @@
const { gql } = require('apollo-server-express')
const gql = require('graphql-tag')
const typeDef = gql`
type IndividualDiscount {

View file

@ -1,4 +1,4 @@
const { gql } = require('apollo-server-express')
const gql = require('graphql-tag')
const typeDef = gql`
type MachineStatus {

View file

@ -1,4 +1,4 @@
const { gql } = require('apollo-server-express')
const gql = require('graphql-tag')
const typeDef = gql`
type Query {

View file

@ -1,4 +1,4 @@
const { gql } = require('apollo-server-express')
const gql = require('graphql-tag')
const typeDef = gql`
type Notification {

View file

@ -1,4 +1,4 @@
const { gql } = require('apollo-server-express')
const gql = require('graphql-tag')
const typeDef = gql`
type Mutation {

View file

@ -1,4 +1,4 @@
const { gql } = require('apollo-server-express')
const gql = require('graphql-tag')
const typeDef = gql`
type Rate {

View file

@ -1,4 +1,4 @@
const { gql } = require('apollo-server-express')
const gql = require('graphql-tag')
const typeDef = gql`
type SanctionMatches {

View file

@ -1,10 +1,10 @@
const { gql } = require('apollo-server-express')
const gql = require('graphql-tag')
const typeDef = gql`
scalar JSON
scalar JSONObject
scalar Date
scalar UploadGQL
scalar Upload
`
module.exports = typeDef

View file

@ -1,4 +1,4 @@
const { gql } = require('apollo-server-express')
const gql = require('graphql-tag')
const typeDef = gql`
type Query {

View file

@ -1,4 +1,4 @@
const { gql } = require('apollo-server-express')
const gql = require('graphql-tag')
const typeDef = gql`
type SMSNotice {

View file

@ -1,4 +1,4 @@
const { gql } = require('apollo-server-express')
const gql = require('graphql-tag')
const typeDef = gql`
type ProcessStatus {

View file

@ -1,4 +1,4 @@
const { gql } = require('apollo-server-express')
const gql = require('graphql-tag')
const typeDef = gql`
type Transaction {

View file

@ -1,4 +1,4 @@
const { gql } = require('apollo-server-express')
const gql = require('graphql-tag')
const typeDef = gql`
type Query {