Verified Commit 9ce2d471 authored by aguestuser's avatar aguestuser

[215] rename `recycleablePhoneNumber` -> `recycleRequest`

parent f2f7db2f
......@@ -10,7 +10,7 @@ const { membershipOf } = require('./models/membership')
const { messageCountOf } = require('./models/messageCount')
const { phoneNumberOf } = require('./models/phoneNumber')
const { smsSenderOf } = require('./models/smsSender')
const { recycleablePhoneNumberOf } = require('./models/recycleablePhoneNumber')
const { recycleRequestOf } = require('./models/recycleRequest')
const { wait } = require('../util')
const { maxConnectionAttempts, connectionInterval } = config
......@@ -30,7 +30,7 @@ const run = async () => {
membership: membershipOf(sequelize, Sequelize),
messageCount: messageCountOf(sequelize, Sequelize),
phoneNumber: phoneNumberOf(sequelize, Sequelize),
recycleablePhoneNumber: recycleablePhoneNumberOf(sequelize, Sequelize),
recycleRequest: recycleRequestOf(sequelize, Sequelize),
smsSender: smsSenderOf(sequelize, Sequelize),
}
......
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('recycleRequests', {
phoneNumber: {
type: Sequelize.STRING,
primaryKey: true,
allowNull: false,
unique: true,
},
createdAt: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.NOW,
},
updatedAt: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.NOW,
},
})
await queryInterface.addIndex('recycleRequests', {
fields: ['createdAt']
})
return queryInterface.dropTable('recycleablePhoneNumbers')
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('recycleRequests')
return queryInterface.createTable('recycleablePhoneNumbers', {
channelPhoneNumber: {
type: Sequelize.STRING,
primaryKey: true,
allowNull: false,
unique: true,
},
whenEnqueued: {
type: Sequelize.DATE,
allowNull: false,
},
createdAt: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.NOW,
},
updatedAt: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.NOW,
},
});
}
};
const { isPhoneNumber } = require('../validations')
const recycleRequestOf = (sequelize, Sequelize) =>
sequelize.define('recycleRequest', {
phoneNumber: {
type: Sequelize.STRING,
primaryKey: true,
allowNull: false,
unique: true,
validate: isPhoneNumber,
},
createdAt: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.NOW,
},
updatedAt: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.NOW,
},
})
module.exports = { recycleRequestOf }
const recycleablePhoneNumberOf = (sequelize, DataTypes) => {
const recycleablePhoneNumber = sequelize.define('recycleablePhoneNumber', {
channelPhoneNumber: {
type: DataTypes.STRING,
primaryKey: true,
allowNull: false,
unique: true,
},
whenEnqueued: {
type: DataTypes.DATE,
allowNull: false,
},
})
return recycleablePhoneNumber
}
module.exports = { recycleablePhoneNumberOf }
import { expect } from 'chai'
import { describe, it, before, after, afterEach } from 'mocha'
import { run } from '../../../../app/db/index'
import { genPhoneNumber } from '../../../support/factories/phoneNumber'
describe('recycleRequest model', () => {
let db
let phoneNumber = genPhoneNumber()
before(async () => (db = await run()))
afterEach(async () => await db.recycleRequest.destroy({ where: {} }))
after(async () => await db.stop())
it('has the correct fields', async () => {
console.log('bar')
const recycleRequest = await db.recycleRequest.create({ phoneNumber })
expect(recycleRequest.phoneNumber).to.be.a('string')
expect(recycleRequest.createdAt).to.be.a('Date')
expect(recycleRequest.updatedAt).to.be.a('Date')
})
describe('validations', () => {
it('requires a phoneNumber', async () => {
const err = await db.recycleRequest.create({ phoneNumber: undefined }).catch(e => e)
expect(err.message).to.include('phoneNumber cannot be null')
})
it('requires phone number to have valid e164 format', async () => {
const err = await db.recycleRequest.create({ phoneNumber: 'foobar' }).catch(e => e)
expect(err.message).to.include('Validation error')
})
it("doesn't allow the same phone number to be enqueued twice", async () => {
await db.recycleRequest.create({ phoneNumber })
const err = await db.recycleRequest.create({ phoneNumber }).catch(e => e)
expect(err.name).to.equal('SequelizeUniqueConstraintError')
})
})
})
import { expect } from 'chai'
import { describe, it, before, after } from 'mocha'
import { run } from '../../../../app/db/index'
import { genPhoneNumber } from '../../../support/factories/phoneNumber'
describe('recycleablePhoneNumber model', () => {
let db
let channelPhoneNumber = genPhoneNumber()
before(async () => {
db = await run()
})
after(async () => {
await db.recycleablePhoneNumber.destroy({ where: {} })
await db.stop()
})
it('has the correct fields', async () => {
const enqueuedChannelNumber = await db.recycleablePhoneNumber.create({
channelPhoneNumber,
whenEnqueued: new Date().toISOString(),
})
expect(enqueuedChannelNumber.channelPhoneNumber).to.be.a('string')
expect(enqueuedChannelNumber.whenEnqueued).to.be.a('Date')
})
describe('validations', () => {
it('requires a channelPhoneNumber', async () => {
const err = await db.recycleablePhoneNumber
.create({ whenEnqueued: new Date().toISOString() })
.catch(e => e)
expect(err.message).to.include('channelPhoneNumber cannot be null')
})
it('requires a timestamp for whenEnqueued', async () => {
const err = await db.recycleablePhoneNumber
.create({ channelPhoneNumber: genPhoneNumber() })
.catch(e => e)
expect(err.message).to.include('whenEnqueued cannot be null')
})
it("doesn't allow the same phone number to be enqueued twice", async () => {
const err = await db.recycleablePhoneNumber
.create({
channelPhoneNumber,
whenEnqueued: new Date().toISOString(),
})
.catch(e => JSON.stringify(e.errors[0]))
expect(err).to.include('channelPhoneNumber must be unique')
})
})
})
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment