Skip to content

ajmasia/coupon-generator

Repository files navigation

Developer Roadmap

Coupons generator

Generating coupon codes easily

Table of content


Install

To test the app, please follow the next steps:

Clone this repo

git clone https://github.com/ajmasia/couponGenerator.git

git clone [email protected]:ajmasia/couponGenerator.git

Run app locally

Install app dependences:

npm i

Run app:

npm start

Build app

npm run build

Run app with docker

Install app dependences:

npm run docker:install

Run app without logs

docker-compose up -d

Run app with logs

docker-compose up

Test the app in your browser http://localhost:3000.

You can see the results both in the browser window and the browserconsole.

Documentaion

You can use two different types of algorithms to generate coupons codes, secuentila and alphanumeric. To do it you just have to configure the service through the config.ts file. The algorithm type is selected in the algorithm property from the configuration file.

const config: IConfig = {
  algorithm: 'alphanumeric',
}

Secuential

Generate numerical coupons sequentially. You can optionally configure the following parameters: amount, digits and startWith:

const config: IConfig = {
  algorithm: 'secuential',
  config: {
    amount: 12,
    digits: 8,
    startWith: 345,
  }
}

By default, undefined parameters have the following values:

const config: IConfig = {
  algorithm: 'secuential',
  config: {
    amount: 5,
    digits: 5,
    startWith: 1,
  }
}

Alphanumeric

Generate alphanumeric coupons. You can optionally configure the following parameters: amount, digits and pattern:

const config: IConfig = {
  algorithm: 'alphanumeric',
  config: {
    amount: 5,
    digits: 8,
    pattern: '#A!',
  }
}

By default, undefined parameters have the following values:

const config: IConfig = {
  algorithm: 'secuential',
  config: {
    amount: 5,
    digits: 5,
    startWith: '#A',
  }
}

Patterns

  • '#' Use numerics values
  • 'A' Use capital letters
  • 'a' Used lowercase letters
  • '!' Use special chars like !@#$ ...

You can combine these patterns. For example #a will generate codes using numbers and lowercase letters.

Add new algorithms to service

All algorithms are stored in Algortuhms folder. To add new one, yo need to a new file with the new algorithm name implemente the new class, following the same pattern as the existing ones:

class Secuential2 implements IAlgorithm {
  public getCodes(config: any): string[] {
    const { amount = 5, digits = 5, startWith = 1 } = config
    const maxNumber = getHighestNumOf(digits)

    if (startWith > maxNumber - amount + 1) {
      return ['Error: Initial value it is higher than allowed']
    }

    const result: string[] = Array.from(Array(amount), (x, index) => {
      const code: number = index + startWith

      return padNumber(code.toString(), digits)
    })

    return result
  }
}

export default Secuential2

After create the new file you need add it to the algorithms index file:

export { default as secuential } from './Secuential'
export { default as alphanumeric } from './Alphanumeric'
export { default as secuential2 } from './Alphanumeric'

From now on it can be used through the configuration file, passing all the configuration parameters through the config property:

const config: IConfig = {
  algorithm: 'secuential2',
  config: {
    amount: 5,
    digits: 5,
    startWith: '#A',
  }
}

Technologies

This project use the next tecnologies trying to use SOLID principles:

Changelog

Version 0.1.3

  • Implement strategy pattern

Version 0.1.2

  • Refactor code and fix some type errors. Now we can custom the file config.
  • Update documentation

Verion 0.1.1

  • This version includes the posibility to use two different algoritms defined on the conf file. Now you can select secuential or alphanumeric algorithms:

    export default {
      algorithm: 'alphanumeric',
      amount: 5,
      digits: 5,
      initialValue: 1,
      pattern: '#A',
    }
  • Update documentation

Version 0.1.0

  • This first versión generate secuentials coupons using a simple configuration file. It is necessary to configure the service by entering the neneeded values in services/couponsGenerator/config.ts:

    export default {
      algorithm: 'secuential',
      amount: 5,
      digits: 5,
      initialValue: 1,
    }

    All this values al required.


License

About

Technical assessments

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published