|
2 | 2 | /* eslint-disable no-underscore-dangle */ |
3 | 3 |
|
4 | 4 | import { connect, disconnect } from '../db/connection'; |
5 | | -import { Companies } from '../db/models'; |
6 | | -import { companyFactory, fieldFactory } from '../db/factories'; |
| 5 | +import { Companies, Customers, InternalNotes, ActivityLogs } from '../db/models'; |
| 6 | +import { |
| 7 | + companyFactory, |
| 8 | + fieldFactory, |
| 9 | + internalNoteFactory, |
| 10 | + activityLogFactory, |
| 11 | + customerFactory, |
| 12 | +} from '../db/factories'; |
| 13 | +import { COC_CONTENT_TYPES } from '../data/constants'; |
7 | 14 |
|
8 | 15 | beforeAll(() => connect()); |
9 | 16 |
|
@@ -121,4 +128,158 @@ describe('Companies model tests', () => { |
121 | 128 | expect(e.message).toBe(`${field.text}: Invalid number`); |
122 | 129 | } |
123 | 130 | }); |
| 131 | + |
| 132 | + test('Update company customers', async () => { |
| 133 | + const customerIds = ['12313qwrqwe', '123', '11234']; |
| 134 | + |
| 135 | + await Companies.updateCustomers(_company._id, customerIds); |
| 136 | + |
| 137 | + for (let customerId of customerIds) { |
| 138 | + const customerObj = await Customers.findOne({ _id: customerId }); |
| 139 | + |
| 140 | + expect(customerObj.companyIds).toContain(_company._id); |
| 141 | + } |
| 142 | + }); |
| 143 | + |
| 144 | + test('removeCompany', async () => { |
| 145 | + const company = await companyFactory({}); |
| 146 | + await customerFactory({ companyIds: [company._id] }); |
| 147 | + |
| 148 | + await internalNoteFactory({ |
| 149 | + contentType: COC_CONTENT_TYPES.COMPANY, |
| 150 | + contentTypeId: company._id, |
| 151 | + }); |
| 152 | + |
| 153 | + const removed = await Companies.removeCompany(company._id); |
| 154 | + |
| 155 | + const internalNote = await InternalNotes.find({ |
| 156 | + contentType: COC_CONTENT_TYPES.COMPANY, |
| 157 | + contentTypeId: company._id, |
| 158 | + }); |
| 159 | + |
| 160 | + const activityLog = await ActivityLogs.find({ |
| 161 | + coc: { |
| 162 | + type: COC_CONTENT_TYPES.COMPANY, |
| 163 | + id: company._id, |
| 164 | + }, |
| 165 | + }); |
| 166 | + |
| 167 | + const customers = await Customers.find({ companyIds: { $in: [company._id] } }); |
| 168 | + |
| 169 | + expect(customers).toHaveLength(0); |
| 170 | + expect(internalNote).toHaveLength(0); |
| 171 | + expect(activityLog).toHaveLength(0); |
| 172 | + expect(removed.result).toEqual({ n: 1, ok: 1 }); |
| 173 | + }); |
| 174 | + |
| 175 | + test('mergeCompanies', async () => { |
| 176 | + const testCompany = await companyFactory({ |
| 177 | + tagIds: ['123', '456', '1234'], |
| 178 | + }); |
| 179 | + |
| 180 | + const testCompany2 = await companyFactory({ |
| 181 | + tagIds: ['1231', '123', 'asd12'], |
| 182 | + }); |
| 183 | + |
| 184 | + let testCustomer = await customerFactory({ |
| 185 | + companyIds: [testCompany._id], |
| 186 | + }); |
| 187 | + |
| 188 | + let testCustomer2 = await customerFactory({ |
| 189 | + companyIds: [testCompany2._id], |
| 190 | + }); |
| 191 | + |
| 192 | + const companyIds = [testCompany._id, testCompany2._id]; |
| 193 | + const mergedTagIds = Array.from(new Set(testCompany.tagIds.concat(testCompany2.tagIds))); |
| 194 | + |
| 195 | + // test duplication |
| 196 | + try { |
| 197 | + await Companies.mergeCompanies(companyIds, { name: _company.name }); |
| 198 | + } catch (e) { |
| 199 | + expect(e.message).toBe('Duplicated name'); |
| 200 | + } |
| 201 | + |
| 202 | + await internalNoteFactory({ |
| 203 | + contentType: COC_CONTENT_TYPES.COMPANY, |
| 204 | + contentTypeId: companyIds[0], |
| 205 | + }); |
| 206 | + |
| 207 | + await activityLogFactory({ |
| 208 | + coc: { |
| 209 | + type: COC_CONTENT_TYPES.COMPANY, |
| 210 | + id: companyIds[0], |
| 211 | + }, |
| 212 | + }); |
| 213 | + |
| 214 | + const doc = { |
| 215 | + name: 'Test name', |
| 216 | + website: 'Test webiste', |
| 217 | + size: 230, |
| 218 | + industry: 'Test industry', |
| 219 | + plan: 'Test plan', |
| 220 | + }; |
| 221 | + |
| 222 | + const updatedCompany = await Companies.mergeCompanies(companyIds, doc); |
| 223 | + |
| 224 | + expect(updatedCompany.name).toBe(doc.name); |
| 225 | + expect(updatedCompany.website).toBe(doc.website); |
| 226 | + expect(updatedCompany.size).toBe(doc.size); |
| 227 | + expect(updatedCompany.industry).toBe(doc.industry); |
| 228 | + expect(updatedCompany.plan).toBe(doc.plan); |
| 229 | + |
| 230 | + // Checking old company datas deleted |
| 231 | + expect(await Companies.find({ _id: companyIds[0] })).toHaveLength(0); |
| 232 | + expect(updatedCompany.tagIds).toEqual(expect.arrayContaining(mergedTagIds)); |
| 233 | + |
| 234 | + testCustomer = await Customers.findOne({ _id: testCustomer._id }); |
| 235 | + expect(testCustomer.companyIds).not.toContain(testCompany._id); |
| 236 | + |
| 237 | + testCustomer2 = await Customers.findOne({ _id: testCustomer2._id }); |
| 238 | + expect(testCustomer2.companyIds).not.toContain(testCompany2._id); |
| 239 | + |
| 240 | + let internalNote = await InternalNotes.find({ |
| 241 | + contentType: COC_CONTENT_TYPES.COMPANY, |
| 242 | + contentTypeId: companyIds[0], |
| 243 | + }); |
| 244 | + |
| 245 | + let activityLog = await ActivityLogs.find({ |
| 246 | + coc: { |
| 247 | + type: COC_CONTENT_TYPES.COMPANY, |
| 248 | + id: companyIds[0], |
| 249 | + }, |
| 250 | + }); |
| 251 | + |
| 252 | + expect(internalNote).toHaveLength(0); |
| 253 | + expect(activityLog).toHaveLength(0); |
| 254 | + |
| 255 | + // Checking new company datas updated |
| 256 | + expect(updatedCompany.tagIds).toEqual(expect.arrayContaining(mergedTagIds)); |
| 257 | + |
| 258 | + expect(testCustomer.companyIds).toContain(updatedCompany._id); |
| 259 | + expect(testCustomer2.companyIds).toContain(updatedCompany._id); |
| 260 | + |
| 261 | + internalNote = await InternalNotes.find({ |
| 262 | + contentType: COC_CONTENT_TYPES.COMPANY, |
| 263 | + contentTypeId: updatedCompany._id, |
| 264 | + }); |
| 265 | + |
| 266 | + activityLog = await ActivityLogs.find({ |
| 267 | + coc: { |
| 268 | + type: COC_CONTENT_TYPES.COMPANY, |
| 269 | + id: updatedCompany._id, |
| 270 | + }, |
| 271 | + }); |
| 272 | + |
| 273 | + expect(internalNote).not.toHaveLength(0); |
| 274 | + expect(activityLog).not.toHaveLength(0); |
| 275 | + }); |
| 276 | + |
| 277 | + test('Check Duplication', async () => { |
| 278 | + // check duplication |
| 279 | + try { |
| 280 | + await Companies.checkDuplication({ name: _company.name }, '123132'); |
| 281 | + } catch (e) { |
| 282 | + expect(e.message).toBe('Duplicated name'); |
| 283 | + } |
| 284 | + }); |
124 | 285 | }); |
0 commit comments